diff --git a/RMuseum/Controllers/GanjoorController.cs b/RMuseum/Controllers/GanjoorController.cs index 37133119..ee27ca3f 100644 --- a/RMuseum/Controllers/GanjoorController.cs +++ b/RMuseum/Controllers/GanjoorController.cs @@ -161,6 +161,35 @@ namespace RMuseum.Controllers return Ok(res.Result); } + /// + /// create new poet + /// + /// + /// + [HttpPost] + [Route("poet")] + [Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + SecurableItem.ModifyOperationShortName)] + [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorPoetCompleteViewModel))] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + [ProducesResponseType((int)HttpStatusCode.NotFound)] + public async Task CreatePoet([FromBody] GanjoorPoetViewModel poet) + { + + if (!string.IsNullOrEmpty(poet.ImageUrl)) + { + return BadRequest("Please send an empty image url, if you are trying to change poet image this is not the right method to do it."); + } + + Guid userId = + new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value); + + var res = await _ganjoorService.CreatePoet(poet, userId); + + if (!string.IsNullOrEmpty(res.ExceptionString)) + return BadRequest(res.ExceptionString); + + return Ok(res.Result); + } /// diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index bc23ed06..94fbfdb2 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -711,6 +711,13 @@ + + + create new poet + + + + get poet image @@ -7258,6 +7265,14 @@ + + + create new poet + + + + + chaneg poet image @@ -8647,6 +8662,14 @@ + + + create new poet + + + + + chaneg poet image diff --git a/RMuseum/Services/IGanjoorService.cs b/RMuseum/Services/IGanjoorService.cs index dafc269c..a4043b57 100644 --- a/RMuseum/Services/IGanjoorService.cs +++ b/RMuseum/Services/IGanjoorService.cs @@ -258,6 +258,14 @@ namespace RMuseum.Services /// Task> UpdatePoet(GanjoorPoetViewModel poet, Guid editingUserId); + /// + /// create new poet + /// + /// + /// + /// + Task> CreatePoet(GanjoorPoetViewModel poet, Guid editingUserId); + /// /// chaneg poet image /// diff --git a/RMuseum/Services/Implementation/GanjoorService.cs b/RMuseum/Services/Implementation/GanjoorService.cs index ee5ee6a4..1737fb52 100644 --- a/RMuseum/Services/Implementation/GanjoorService.cs +++ b/RMuseum/Services/Implementation/GanjoorService.cs @@ -2507,7 +2507,7 @@ namespace RMuseum.Services.Implementation { _memoryCache.Remove(cachKeyPoets2); } - + var cacheKeyPoet = $"/api/ganjoor/poet/{poetId}"; if (_memoryCache.TryGetValue(cacheKeyPoet, out GanjoorPoetCompleteViewModel poetCat)) { @@ -2564,11 +2564,8 @@ namespace RMuseum.Services.Implementation await _context.SaveChangesAsync(); - CleanPoetCache(poet.Id); - - return new RServiceResult(true); } catch(Exception exp) @@ -2577,6 +2574,111 @@ namespace RMuseum.Services.Implementation } } + /// + /// create new poet + /// + /// + /// + /// + public async Task> CreatePoet(GanjoorPoetViewModel poet, Guid editingUserId) + { + try + { + if(await _context.GanjoorPoets.Where(p => p.Nickname == poet.Nickname || p.Name == poet.Name).AnyAsync()) + { + return new RServiceResult(null, "conflicting poet"); + } + + if(await _context.GanjoorCategories.Where(c => c.FullUrl == poet.FullUrl).AnyAsync()) + { + return new RServiceResult(null, "conflicting cat"); + } + + if (await _context.GanjoorPages.Where(p => p.FullUrl == poet.FullUrl).AnyAsync()) + { + return new RServiceResult(null, "conflicting page"); + } + + if(poet.FullUrl.IndexOf('/') != 0) + { + return new RServiceResult(null, "Invalid FullUrl, it must start with /"); + } + + if(poet.FullUrl.Substring(1).IndexOf('/') >= 0) + { + return new RServiceResult(null, "Invalid FullUrl, it must contain only one /"); + } + + var id = 1 + await _context.GanjoorPoets.MaxAsync(p => p.Id); + + GanjoorPoet dbPoet = new GanjoorPoet() + { + Id = id, + Name = poet.Name, + Nickname = poet.Nickname, + Description = poet.Description, + Published = false + }; + + _context.GanjoorPoets.Add(dbPoet); + + var poetCatId = 1 + await _context.GanjoorCategories.MaxAsync(c => c.Id); + + GanjoorCat dbCat = new GanjoorCat() + { + Id = poetCatId, + PoetId = id, + Title = poet.Nickname, + UrlSlug = poet.FullUrl.Substring(1), + FullUrl = poet.FullUrl + }; + _context.GanjoorCategories.Add(dbCat); + + var poetPageId = 1 + await _context.GanjoorPages.MaxAsync(p => p.Id); + + GanjoorPage dbPage = new GanjoorPage() + { + Id = poetPageId, + GanjoorPageType = GanjoorPageType.PoetPage, + Published = false, + PageOrder = -1, + Title = poet.Nickname, + FullTitle = poet.Nickname, + UrlSlug = poet.FullUrl.Substring(1), + FullUrl = poet.FullUrl, + HtmlText = poet.Description, + PoetId = id, + CatId = poetCatId, + PostDate = DateTime.Now + }; + + _context.GanjoorPages.Add(dbPage); + + GanjoorPageSnapshot snapshot = new GanjoorPageSnapshot() + { + GanjoorPageId = poetPageId, + MadeObsoleteByUserId = editingUserId, + RecordDate = DateTime.Now, + Note = "ایجاد شاعر", + Title = dbPage.Title, + UrlSlug = dbPage.UrlSlug, + HtmlText = dbPage.HtmlText, + }; + + _context.GanjoorPageSnapshots.Add(snapshot); + + await _context.SaveChangesAsync(); + + CleanPoetCache(0); + + return await GetPoetById(id); + } + catch (Exception exp) + { + return new RServiceResult(null, exp.ToString()); + } + } + /// /// chaneg poet image ///