#139 create new poet api

This commit is contained in:
Hamid Reza Mohammadi 2021-06-11 09:59:54 +04:30
parent aab63a3848
commit b916c0c351
4 changed files with 166 additions and 4 deletions

View File

@ -161,6 +161,35 @@ namespace RMuseum.Controllers
return Ok(res.Result);
}
/// <summary>
/// create new poet
/// </summary>
/// <param name="poet"></param>
/// <returns></returns>
[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<IActionResult> 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);
}
/// <summary>

View File

@ -711,6 +711,13 @@
<param name="poet"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.CreatePoet(RMuseum.Models.Ganjoor.ViewModels.GanjoorPoetViewModel)">
<summary>
create new poet
</summary>
<param name="poet"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.GetPoetImage(System.String)">
<summary>
get poet image
@ -7258,6 +7265,14 @@
<param name="editingUserId"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.CreatePoet(RMuseum.Models.Ganjoor.ViewModels.GanjoorPoetViewModel,System.Guid)">
<summary>
create new poet
</summary>
<param name="poet"></param>
<param name="editingUserId"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.ChangePoetImage(System.Int32,System.Guid)">
<summary>
chaneg poet image
@ -8647,6 +8662,14 @@
<param name="editingUserId"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.CreatePoet(RMuseum.Models.Ganjoor.ViewModels.GanjoorPoetViewModel,System.Guid)">
<summary>
create new poet
</summary>
<param name="poet"></param>
<param name="editingUserId"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.ChangePoetImage(System.Int32,System.Guid)">
<summary>
chaneg poet image

View File

@ -258,6 +258,14 @@ namespace RMuseum.Services
/// <returns></returns>
Task<RServiceResult<bool>> UpdatePoet(GanjoorPoetViewModel poet, Guid editingUserId);
/// <summary>
/// create new poet
/// </summary>
/// <param name="poet"></param>
/// <param name="editingUserId"></param>
/// <returns></returns>
Task<RServiceResult<GanjoorPoetCompleteViewModel>> CreatePoet(GanjoorPoetViewModel poet, Guid editingUserId);
/// <summary>
/// chaneg poet image
/// </summary>

View File

@ -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<bool>(true);
}
catch(Exception exp)
@ -2577,6 +2574,111 @@ namespace RMuseum.Services.Implementation
}
}
/// <summary>
/// create new poet
/// </summary>
/// <param name="poet"></param>
/// <param name="editingUserId"></param>
/// <returns></returns>
public async Task<RServiceResult<GanjoorPoetCompleteViewModel>> CreatePoet(GanjoorPoetViewModel poet, Guid editingUserId)
{
try
{
if(await _context.GanjoorPoets.Where(p => p.Nickname == poet.Nickname || p.Name == poet.Name).AnyAsync())
{
return new RServiceResult<GanjoorPoetCompleteViewModel>(null, "conflicting poet");
}
if(await _context.GanjoorCategories.Where(c => c.FullUrl == poet.FullUrl).AnyAsync())
{
return new RServiceResult<GanjoorPoetCompleteViewModel>(null, "conflicting cat");
}
if (await _context.GanjoorPages.Where(p => p.FullUrl == poet.FullUrl).AnyAsync())
{
return new RServiceResult<GanjoorPoetCompleteViewModel>(null, "conflicting page");
}
if(poet.FullUrl.IndexOf('/') != 0)
{
return new RServiceResult<GanjoorPoetCompleteViewModel>(null, "Invalid FullUrl, it must start with /");
}
if(poet.FullUrl.Substring(1).IndexOf('/') >= 0)
{
return new RServiceResult<GanjoorPoetCompleteViewModel>(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<GanjoorPoetCompleteViewModel>(null, exp.ToString());
}
}
/// <summary>
/// chaneg poet image
/// </summary>