From bd6f351c8a8df72f5a549ad472679fc988f32c5b Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Fri, 7 Sep 2018 19:16:29 +0000 Subject: [PATCH] --- devdocs/todo.txt | 5 +- .../AyaNova/Controllers/TagGroupController.cs | 59 +++++++++++++++++++ .../AyaNova/Controllers/TagMapController.cs | 4 +- server/AyaNova/biz/TagGroupBiz.cs | 25 +++++++- server/AyaNova/biz/TagMapBiz.cs | 4 ++ server/AyaNova/models/dto/TagMapGroupInfo.cs | 15 +++++ server/AyaNova/models/dto/TagMapInfo.cs | 2 +- 7 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 server/AyaNova/models/dto/TagMapGroupInfo.cs diff --git a/devdocs/todo.txt b/devdocs/todo.txt index b94c267b..0473c9d5 100644 --- a/devdocs/todo.txt +++ b/devdocs/todo.txt @@ -37,7 +37,10 @@ IMMEDIATE ITEMS: - TESTS: Make a test for each type of group operation from biz point of view - TAGMAP: CREATEASYNC code to tag an item should take into account if it's already tagged and not make a new record, just return existing - TAGGROUPMAP: add tagGROUPMAP CREATEASYNC should take into account if already added //TODO: see if already present and just return inObj if so - + - Need an ObjectExists type object for checking if something exists when specified by type and ID + - Could this be a combined method to get the name as well just to save time? + - Or should that be another method (YES, first code a translator to translate types to db tables (however the fuck that works with EF), then can use it in turn to verify existance and get name separately) + - Once we have that go back into any code that accepts a typeandid and add to Validation code to check for it - Localized text - ** DEVISE a system to ensure no unused keys are brought forward to raven - Search and search text indexing diff --git a/server/AyaNova/Controllers/TagGroupController.cs b/server/AyaNova/Controllers/TagGroupController.cs index f2755648..333afd00 100644 --- a/server/AyaNova/Controllers/TagGroupController.cs +++ b/server/AyaNova/Controllers/TagGroupController.cs @@ -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 + + /// + /// Post TagMap from group - Map a group of tags to an object / Id + /// Required roles: Same roles as tagged object + /// + /// + /// TagMapGroupInfo + /// object + [HttpPost] + public async Task 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 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); diff --git a/server/AyaNova/Controllers/TagMapController.cs b/server/AyaNova/Controllers/TagMapController.cs index e242082b..5c6cad5d 100644 --- a/server/AyaNova/Controllers/TagMapController.cs +++ b/server/AyaNova/Controllers/TagMapController.cs @@ -89,9 +89,9 @@ namespace AyaNova.Api.Controllers /// - /// 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 /// /// TagMapInfo /// object diff --git a/server/AyaNova/biz/TagGroupBiz.cs b/server/AyaNova/biz/TagGroupBiz.cs index eb18e5e8..513bc83c 100644 --- a/server/AyaNova/biz/TagGroupBiz.cs +++ b/server/AyaNova/biz/TagGroupBiz.cs @@ -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 l = new List(); l = await ct.Tag .Where(m => tagGroupTags.Contains(m.Id)) @@ -220,6 +220,29 @@ namespace AyaNova.Biz } + //////////////////////////////////////////////////////////////////////////////////////////////// + //TagObject + internal async Task> TagObject(TagMapGroupInfo inObj) + { + var ReturnObject = new List(); + 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 // diff --git a/server/AyaNova/biz/TagMapBiz.cs b/server/AyaNova/biz/TagMapBiz.cs index 47d08879..65dc13f9 100644 --- a/server/AyaNova/biz/TagMapBiz.cs +++ b/server/AyaNova/biz/TagMapBiz.cs @@ -35,6 +35,7 @@ namespace AyaNova.Biz //CREATE internal async Task 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"); diff --git a/server/AyaNova/models/dto/TagMapGroupInfo.cs b/server/AyaNova/models/dto/TagMapGroupInfo.cs new file mode 100644 index 00000000..9d60479d --- /dev/null +++ b/server/AyaNova/models/dto/TagMapGroupInfo.cs @@ -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; } + } + + +} diff --git a/server/AyaNova/models/dto/TagMapInfo.cs b/server/AyaNova/models/dto/TagMapInfo.cs index 4c3eba24..9a14472d 100644 --- a/server/AyaNova/models/dto/TagMapInfo.cs +++ b/server/AyaNova/models/dto/TagMapInfo.cs @@ -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; }