This commit is contained in:
2018-09-07 19:16:29 +00:00
parent d2783cf626
commit bd6f351c8a
7 changed files with 109 additions and 5 deletions

View File

@@ -1,5 +1,6 @@
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
@@ -400,6 +401,64 @@ namespace AyaNova.Api.Controllers
/// <summary>
/// Post TagMap from group - Map a group of tags to an object / Id
/// Required roles: Same roles as tagged object
///
/// </summary>
/// <param name="inObj">TagMapGroupInfo</param>
/// <returns><see cref="TagMap"/> object</returns>
[HttpPost]
public async Task<IActionResult> PostTagMap([FromBody] TagMapGroupInfo inObj)
{
if (!serverState.IsOpen)
{
return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason));
}
//If a user has change roles, or editOwnRoles then they can create, true is passed for isOwner since they are creating so by definition the owner
if (!Authorized.IsAuthorizedToCreate(HttpContext.Items, AyaType.TagMap))
{
return StatusCode(401, new ApiNotAuthorizedResponse());
}
//Rights to parent taggable object?
if (!Authorized.IsAuthorizedToCreate(HttpContext.Items, inObj.TagToObjectType))
{
return StatusCode(401, new ApiNotAuthorizedResponse());
}
if (!ModelState.IsValid)
{
return BadRequest(new ApiErrorResponse(ModelState));
}
//Instantiate the business object handler
TagGroupBiz biz = new TagGroupBiz(ct, UserIdFromContext.Id(HttpContext.Items), UserRolesFromContext.Roles(HttpContext.Items));
//Create and return list
List<NameIdItem> l = await biz.TagObject(inObj);
if (l == null)
{
//error return
return BadRequest(new ApiErrorResponse(biz.Errors));
}
else
{
//save and success return
await ct.SaveChangesAsync();
//BIZLOG: Not going to log this for now, it's too common an operation and would require bringing in more info. If decide to implement should log the parent object with text of tag instead
//and don't forget about import from v7 as well
return Ok(new ApiOkResponse(l));
}
}
private bool TagGroupExists(long id)
{
return ct.TagGroup.Any(e => e.Id == id);

View File

@@ -89,9 +89,9 @@ namespace AyaNova.Api.Controllers
/// <summary>
/// Post TagMap - Map a tag to an object / Id
/// Post TagMap - Map a single Tag to an object / Id
/// Required roles: Same roles as tagged object
///
/// Required roles: Same roles as tagged object
/// </summary>
/// <param name="inObj">TagMapInfo</param>
/// <returns><see cref="TagMap"/> object</returns>

View File

@@ -147,7 +147,7 @@ namespace AyaNova.Biz
{
//first, get an array of the tagId's in this group
var tagGroupTags = await ct.TagGroupMap.Where(m => m.TagGroupId == tagGroupId).Select(m => m.TagId).ToArrayAsync();
List<NameIdItem> l = new List<NameIdItem>();
l = await ct.Tag
.Where(m => tagGroupTags.Contains(m.Id))
@@ -220,6 +220,29 @@ namespace AyaNova.Biz
}
////////////////////////////////////////////////////////////////////////////////////////////////
//TagObject
internal async Task<List<NameIdItem>> TagObject(TagMapGroupInfo inObj)
{
var ReturnObject = new List<NameIdItem>();
var TagGroupTags = await GetTagsInGroupPickListAsync(inObj.TagGroupId);
if (TagGroupTags.Count == 0) return ReturnObject;
//Tag each one separately via TagMap which handles cases where object is already tagged with that tag etc
var MapBiz = new TagMapBiz(ct, userId, userRoles);
foreach (NameIdItem TagInGroup in TagGroupTags)
{
var TagMapItem = await MapBiz.CreateAsync(new TagMapInfo { TagId = TagInGroup.Id, TagToObjectId=inObj.TagToObjectId, TagToObjectType=inObj.TagToObjectType });
ReturnObject.Add(TagInGroup);
}
await ct.SaveChangesAsync();
return ReturnObject;
}
////////////////////////////////////////////////////////////////////////////////////////////////
//VALIDATION
//

View File

@@ -35,6 +35,7 @@ namespace AyaNova.Biz
//CREATE
internal async Task<TagMap> CreateAsync(TagMapInfo inObj)
{
//TODO: Does object exist before attempting to tag it??
Validate(inObj, true);
if (HasErrors)
@@ -140,6 +141,9 @@ namespace AyaNova.Biz
{
//run validation and biz rules
// //Name required
// if (string.IsNullOrWhiteSpace(inObj))
// AddError(ValidationErrorType.RequiredPropertyEmpty, "Name");

View File

@@ -0,0 +1,15 @@
using AyaNova.Biz;
namespace AyaNova.Models
{
//Used by TagGroup controller to map a group of tags to an object
public class TagMapGroupInfo
{
public long TagGroupId { get; set; }
public long TagToObjectId { get; set; }
public AyaType TagToObjectType { get; set; }
}
}

View File

@@ -2,7 +2,7 @@ using AyaNova.Biz;
namespace AyaNova.Models
{
//Used by Tag controller to map a tag to an object
public class TagMapInfo
{
public long TagId { get; set; }