From cd787605f348405001f4948bf72d4862f54dfa3e Mon Sep 17 00:00:00 2001 From: Daniel Gyulai Date: Tue, 24 Nov 2020 11:16:41 +0100 Subject: [PATCH] Exposed priority change as separate API endpoint --- CoviDok/Api/Request/CasePriority.cs | 14 ++ CoviDok/Api/Request/CaseUpdate.cs | 1 - CoviDok/BLL/User/Handlers/ICaseHandler.cs | 56 ++--- CoviDok/BLL/User/Managers/CaseManager.cs | 21 +- CoviDok/Controllers/CaseController.cs | 21 +- CoviDok/Data/MySQL/MySqlCaseHandler.cs | 262 +++++++++++----------- 6 files changed, 216 insertions(+), 159 deletions(-) create mode 100644 CoviDok/Api/Request/CasePriority.cs diff --git a/CoviDok/Api/Request/CasePriority.cs b/CoviDok/Api/Request/CasePriority.cs new file mode 100644 index 0000000..5a0f2b8 --- /dev/null +++ b/CoviDok/Api/Request/CasePriority.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace CoviDok.Api.Request +{ + public class CasePriority + { + public int CaseId { get; set; } + public string SessionId { get; set; } + public Priority Priority { get; set; } + } +} diff --git a/CoviDok/Api/Request/CaseUpdate.cs b/CoviDok/Api/Request/CaseUpdate.cs index 4b839b3..047da61 100644 --- a/CoviDok/Api/Request/CaseUpdate.cs +++ b/CoviDok/Api/Request/CaseUpdate.cs @@ -11,6 +11,5 @@ namespace CoviDok.Api.Request public string UpdateMsg { get; set; } public List Images {get;set;} public string SessionId { get; set; } - public Priority Priority { get; set; } } } diff --git a/CoviDok/BLL/User/Handlers/ICaseHandler.cs b/CoviDok/BLL/User/Handlers/ICaseHandler.cs index 16e382d..d1a04dd 100644 --- a/CoviDok/BLL/User/Handlers/ICaseHandler.cs +++ b/CoviDok/BLL/User/Handlers/ICaseHandler.cs @@ -1,27 +1,29 @@ -using CoviDok.Api.Request; -using CoviDok.BLL.User.Managers; -using CoviDok.Data.Model; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace CoviDok.BLL.User.Handlers -{ - public interface ICaseHandler - { - public Task> Filter(CaseFilter filter); - public Task GetCase(int id); - - public Task AddCase(Case c); - - public Task UpdateCase(int id, Case Case, Update update); - - public Task SetCase(int id, CaseStatus status, Update message); - public bool IsAuthorized(int Id, Case c); - - public List GetUpdatesForCase(int id); - public Update GetUpadte(int id); - public bool IsAssistantOfDoctor(int id, int doctorId); - } -} +using CoviDok.Api; +using CoviDok.Api.Request; +using CoviDok.BLL.User.Managers; +using CoviDok.Data.Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace CoviDok.BLL.User.Handlers +{ + public interface ICaseHandler + { + public Task> Filter(CaseFilter filter); + public Task GetCase(int id); + + public Task AddCase(Case c); + + public Task UpdateCase(int id, Case Case, Update update); + + public Task SetCase(int id, CaseStatus status, Update message); + public bool IsAuthorized(int Id, Case c); + + public List GetUpdatesForCase(int id); + public Update GetUpadte(int id); + public Task SetPriority(int id, Priority priority); + public bool IsAssistantOfDoctor(int id, int doctorId); + } +} diff --git a/CoviDok/BLL/User/Managers/CaseManager.cs b/CoviDok/BLL/User/Managers/CaseManager.cs index 98c8634..115e56a 100644 --- a/CoviDok/BLL/User/Managers/CaseManager.cs +++ b/CoviDok/BLL/User/Managers/CaseManager.cs @@ -1,4 +1,5 @@ -using CoviDok.Api.Request; +using CoviDok.Api; +using CoviDok.Api.Request; using CoviDok.BLL.Sessions; using CoviDok.BLL.Storage; using CoviDok.BLL.User.Handlers; @@ -113,6 +114,20 @@ namespace CoviDok.BLL.User.Managers } } + public async Task SetPriority(Session s, int id, Priority priority) + { + Case c = await handler.GetCase(id); + if (c == null) throw new KeyNotFoundException(); + if (handler.IsAuthorized(s.Id, c)) + { + await handler.SetPriority(id, priority); + } + else + { + throw new UnauthorizedAccessException(); + } + } + public async Task CreateCase(Session s, int DoctorId, int ChildId, string Title, DateTime startDate, Api.Priority priority) { // TODO szülő csak saját gyereket jelenthet @@ -131,7 +146,7 @@ namespace CoviDok.BLL.User.Managers return await handler.AddCase(c); } - public async Task UpdateCase(Session s, int id, string updateMsg, List Images, Api.Priority priority) + public async Task UpdateCase(Session s, int id, string updateMsg, List Images) { Case c = await handler.GetCase(id); if (c == null) throw new KeyNotFoundException("Case Id not found: " + id); @@ -146,7 +161,7 @@ namespace CoviDok.BLL.User.Managers if (s.Id == c.ParentId) c.Assignee = c.DoctorId; // Ha szülő updatel, az assignee az orvos lesz // TODO Ha a doki VAGY asszisztense frissít if (s.Id == c.DoctorId) c.Assignee = c.ParentId; // Ha doki frissít, a szülőhöz kerül - c.Priority = priority; + c.LastModificationDate = DateTime.Now; Update update = new Update { CaseId = c.Id, diff --git a/CoviDok/Controllers/CaseController.cs b/CoviDok/Controllers/CaseController.cs index bf98ba4..f1fe32d 100644 --- a/CoviDok/Controllers/CaseController.cs +++ b/CoviDok/Controllers/CaseController.cs @@ -41,6 +41,25 @@ namespace CoviDok.Controllers } } + [HttpPost("{id}/priority")] + public async Task PostPriorityChange(int id, CasePriority priority) + { + try + { + Session s = await Handler.GetSession(priority.SessionId); + await mgr.SetPriority(s, id, priority.Priority); + return Ok(); + } + catch (KeyNotFoundException) + { + return NotFound(); + } + catch (UnauthorizedAccessException) + { + return Unauthorized(); + } + } + // POST /api/Case/{id}/update [HttpPut("{id}/update")] @@ -49,7 +68,7 @@ namespace CoviDok.Controllers try { Session s = await Handler.GetSession(data.SessionId); - await mgr.UpdateCase(s, id, data.UpdateMsg, data.Images, data.Priority); + await mgr.UpdateCase(s, id, data.UpdateMsg, data.Images); return Ok(); } catch (UnauthorizedAccessException) diff --git a/CoviDok/Data/MySQL/MySqlCaseHandler.cs b/CoviDok/Data/MySQL/MySqlCaseHandler.cs index 0a68981..fb527ce 100644 --- a/CoviDok/Data/MySQL/MySqlCaseHandler.cs +++ b/CoviDok/Data/MySQL/MySqlCaseHandler.cs @@ -1,127 +1,135 @@ -using CoviDok.Api.Request; -using CoviDok.BLL; -using CoviDok.BLL.User.Handlers; -using CoviDok.BLL.User.Managers; -using CoviDok.Data.Model; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Threading.Tasks; - -namespace CoviDok.Data.MySQL -{ - public class MySqlCaseHandler : ICaseHandler - { - private readonly MySqlContext context = new MySqlContext(); - public async Task AddCase(Case c) - { - context.Cases.Add(c); - await context.SaveChangesAsync(); - return c; - } - - public async Task SetCase(int id, CaseStatus status, Update message) - { - Case c = await context.Cases.FindAsync(id); - message.CaseId = c.Id; - context.Updates.Add(message); - c.CaseStatus = status; - c.LastModificationDate = DateTime.Now; - c.Updates.Add(message); - await context.SaveChangesAsync(); - } - - public List GetUpdatesForCase(int id) - { - List updates = (from u in context.Updates where u.CaseId == id select u).ToList(); - foreach (Update update in updates) - { - update.Images = (from i in context.Images where i.UpdateId == update.Id select i.ImageId).ToList(); - } - return updates; - } - - public Update GetUpadte(int id) - { - Update update = (from u in context.Updates where u.Id == id select u).First(); - if (update != null) - { - update.Images = (from i in context.Images where i.UpdateId == update.Id select i.ImageId).ToList(); - } - return update; - } - - public bool IsAssistantOfDoctor(int id, int doctorId) - { - return context.Assistants.Any( (a) => a.Id == id && a.DoctorId == doctorId); - } - - public async Task> Filter(CaseFilter filter) - { - List ret = new List(); - - await Task.Run(() => { - var query = from c in context.Cases select c; - if (filter.ChildId != int.MinValue) - { - query = query.Where(c => c.ChildId == filter.ChildId); - } - if (filter.DoctorId != int.MinValue) - { - query = query.Where(c => c.DoctorId == filter.DoctorId); - } - if (filter.ParentId != int.MinValue) - { - query = query.Where(c => c.ParentId == filter.ParentId); - } - if (filter.Assignee != int.MinValue) - { - query = query.Where(c => c.Assignee == filter.Assignee); - } - if (filter.Title != null) - { - query = query.Where(c => c.Title.Contains(filter.Title)); - } - ret = query.ToList(); - }); - return ret; - } - - public async Task GetCase(int id) - { - return await context.Cases.FindAsync(id); - } - - public async Task UpdateCase(int id, Case Case, Update update) - { - Case c = await context.Cases.FindAsync(id); - context.Entry(c).State = Microsoft.EntityFrameworkCore.EntityState.Modified; - string[] forbidden = { "Updates" }; - PropertyCopier.Copy(Case, c, forbidden); - context.Updates.Add(update); - await context.SaveChangesAsync(); - foreach (string ImageId in update.Images) - { - Image image = new Image - { - UpdateId = update.Id, - ImageId = ImageId - }; - context.Images.Add(image); - } - update.CreatedDate = DateTime.Now; - c.Updates.Add(update); - await context.SaveChangesAsync(); - } - - public bool IsAuthorized(int Id, Case c) - { - if (Id == c.DoctorId || Id == c.ParentId) return true; - // Ha van olyan Asszisztens, akinek; - // - a dokija egyezik az ügy dokijával - // - azonosítója a bejelentezett user azonosítója - return (context.Assistants.Any(a => a.Id == Id && a.DoctorId == c.DoctorId)); - } - } -} +using CoviDok.Api; +using CoviDok.Api.Request; +using CoviDok.BLL; +using CoviDok.BLL.User.Handlers; +using CoviDok.BLL.User.Managers; +using CoviDok.Data.Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Threading.Tasks; + +namespace CoviDok.Data.MySQL +{ + public class MySqlCaseHandler : ICaseHandler + { + private readonly MySqlContext context = new MySqlContext(); + public async Task AddCase(Case c) + { + context.Cases.Add(c); + await context.SaveChangesAsync(); + return c; + } + + public async Task SetCase(int id, CaseStatus status, Update message) + { + Case c = await context.Cases.FindAsync(id); + message.CaseId = c.Id; + context.Updates.Add(message); + c.CaseStatus = status; + c.LastModificationDate = DateTime.Now; + c.Updates.Add(message); + await context.SaveChangesAsync(); + } + + public async Task SetPriority(int id, Priority priority) + { + Case c = await context.Cases.FindAsync(id); + c.Priority = priority; + await context.SaveChangesAsync(); + } + + public List GetUpdatesForCase(int id) + { + List updates = (from u in context.Updates where u.CaseId == id select u).ToList(); + foreach (Update update in updates) + { + update.Images = (from i in context.Images where i.UpdateId == update.Id select i.ImageId).ToList(); + } + return updates; + } + + public Update GetUpadte(int id) + { + Update update = (from u in context.Updates where u.Id == id select u).First(); + if (update != null) + { + update.Images = (from i in context.Images where i.UpdateId == update.Id select i.ImageId).ToList(); + } + return update; + } + + public bool IsAssistantOfDoctor(int id, int doctorId) + { + return context.Assistants.Any( (a) => a.Id == id && a.DoctorId == doctorId); + } + + public async Task> Filter(CaseFilter filter) + { + List ret = new List(); + + await Task.Run(() => { + var query = from c in context.Cases select c; + if (filter.ChildId != int.MinValue) + { + query = query.Where(c => c.ChildId == filter.ChildId); + } + if (filter.DoctorId != int.MinValue) + { + query = query.Where(c => c.DoctorId == filter.DoctorId); + } + if (filter.ParentId != int.MinValue) + { + query = query.Where(c => c.ParentId == filter.ParentId); + } + if (filter.Assignee != int.MinValue) + { + query = query.Where(c => c.Assignee == filter.Assignee); + } + if (filter.Title != null) + { + query = query.Where(c => c.Title.Contains(filter.Title)); + } + ret = query.ToList(); + }); + return ret; + } + + public async Task GetCase(int id) + { + return await context.Cases.FindAsync(id); + } + + public async Task UpdateCase(int id, Case Case, Update update) + { + Case c = await context.Cases.FindAsync(id); + context.Entry(c).State = Microsoft.EntityFrameworkCore.EntityState.Modified; + string[] forbidden = { "Updates" }; + PropertyCopier.Copy(Case, c, forbidden); + context.Updates.Add(update); + await context.SaveChangesAsync(); + foreach (string ImageId in update.Images) + { + Image image = new Image + { + UpdateId = update.Id, + ImageId = ImageId + }; + context.Images.Add(image); + } + update.CreatedDate = DateTime.Now; + c.Updates.Add(update); + await context.SaveChangesAsync(); + } + + public bool IsAuthorized(int Id, Case c) + { + if (Id == c.DoctorId || Id == c.ParentId) return true; + // Ha van olyan Asszisztens, akinek; + // - a dokija egyezik az ügy dokijával + // - azonosítója a bejelentezett user azonosítója + return (context.Assistants.Any(a => a.Id == Id && a.DoctorId == c.DoctorId)); + } + } +}