From 8bd68fbe2d3a6df6bf6a16c350f2f7664b14182e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20W=C3=BCrtz?= Date: Wed, 22 Nov 2023 14:26:28 +0100 Subject: [PATCH] remove all use of girafdbcontext in week template controller + setup weektemplate repository --- .../Controllers/WeekTemplateController.cs | 51 ++++++---------- .../Interfaces/IWeekTemplateRepository.cs | 17 +++++- .../WeekPlanner/WeekTemplateRepository.cs | 59 ++++++++++++++++++- 3 files changed, 90 insertions(+), 37 deletions(-) diff --git a/GirafAPI/Controllers/WeekTemplateController.cs b/GirafAPI/Controllers/WeekTemplateController.cs index 08dd305d..5ea9086e 100644 --- a/GirafAPI/Controllers/WeekTemplateController.cs +++ b/GirafAPI/Controllers/WeekTemplateController.cs @@ -2,7 +2,7 @@ using GirafEntities.User; using GirafEntities.WeekPlanner; using GirafEntities.WeekPlanner.DTOs; -using GirafRepositories.Persistence; +using GirafRepositories.WeekPlanner.Interfaces; using GirafServices.Authentication; using GirafServices.User; using GirafServices.WeekPlanner; @@ -11,7 +11,6 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using System.Collections.Generic; -using System.Linq; using System.Threading.Tasks; namespace GirafAPI.Controllers @@ -36,10 +35,8 @@ public class WeekTemplateController : Controller /// private readonly IAuthenticationService _authentication; - // SHOULD BE REMOVED AFTER REFACTORING OF THIS CONTROLLER HAS BEEN COMPLETED! - private readonly GirafDbContext _context; - - + private readonly IWeekTemplateRepository _weekTemplateRepository; + /// /// Constructor is called by the asp.net runtime. /// @@ -49,14 +46,15 @@ public class WeekTemplateController : Controller public WeekTemplateController(IGirafService giraf, ILoggerFactory loggerFactory, IAuthenticationService authentication, - GirafDbContext context, + IWeekTemplateRepository weekTemplateRepository, IWeekService weekService, - IDepartmentService departmentService) + IDepartmentService departmentService + ) { _giraf = giraf; _giraf._logger = loggerFactory.CreateLogger("WeekTemplate"); _authentication = authentication; - _context = context; + _weekTemplateRepository = weekTemplateRepository; _weekService = weekService; _departmentService = departmentService; } @@ -79,9 +77,7 @@ public async Task GetWeekTemplates() if (!await _authentication.HasTemplateAccess(user)) return StatusCode(StatusCodes.Status403Forbidden, new ErrorResponse(ErrorCode.NotAuthorized, "User does not have permission")); - var result = _context.WeekTemplates - .Where(t => t.DepartmentKey == user.DepartmentKey) - .Select(w => new WeekTemplateNameDTO(w.Name, w.Id)).ToArray(); + var result = _weekTemplateRepository.GetWeekTemplatesForUser(user); if (result.Length < 1) { @@ -109,14 +105,7 @@ public async Task GetWeekTemplate(long id) { var user = await _giraf.LoadBasicUserDataAsync(HttpContext.User); - var template = await (_context.WeekTemplates - .Include(w => w.Thumbnail) - .Include(u => u.Weekdays) - .ThenInclude(wd => wd.Activities) - .ThenInclude(pa => pa.Pictograms) - .ThenInclude(p => p.Pictogram) - .Where(t => t.DepartmentKey == user.DepartmentKey) - .FirstOrDefaultAsync(w => w.Id == id)); + var template = await _weekTemplateRepository.GetWeekTemplateFromIdAndUser(id, user); if (template == null) return NotFound(new ErrorResponse(ErrorCode.NoWeekTemplateFound, "No week template found")); @@ -162,8 +151,8 @@ public async Task CreateWeekTemplate([FromBody] WeekTemplateDTO te if (errorCode != null) return BadRequest(errorCode); - _context.WeekTemplates.Add(newTemplate); - await _context.SaveChangesAsync(); + _weekTemplateRepository.AddWeekTemplate(newTemplate); + await _weekTemplateRepository.SaveChangesAsync(); return CreatedAtRoute( "GetWeekTemplate", new { id = newTemplate.Id }, @@ -198,12 +187,7 @@ public async Task UpdateWeekTemplate(long id, [FromBody] WeekTempl if (newValuesDto == null) return BadRequest(new ErrorResponse(ErrorCode.MissingProperties, "Missing newValuesDto")); - var template = _context.WeekTemplates - .Include(w => w.Thumbnail) - .Include(u => u.Weekdays) - .ThenInclude(wd => wd.Activities) - .ThenInclude(e => e.Pictograms) - .FirstOrDefault(t => id == t.Id); + var template = _weekTemplateRepository.GetWeekTemplateFromId(id); if (template == null) return NotFound(new ErrorResponse(ErrorCode.WeekTemplateNotFound, "Weektemplate not found")); @@ -215,8 +199,8 @@ public async Task UpdateWeekTemplate(long id, [FromBody] WeekTempl if (errorCode != null) return BadRequest(errorCode); - _context.WeekTemplates.Update(template); - await _context.SaveChangesAsync(); + _weekTemplateRepository.UpdateWeekTemplate(template); + await _weekTemplateRepository.SaveChangesAsync(); return Ok(new SuccessResponse(new WeekTemplateDTO(template))); } @@ -240,8 +224,7 @@ public async Task DeleteTemplate(long id) if (user == null) return Unauthorized(new ErrorResponse(ErrorCode.UserNotFound, "User not found")); - var template = _context.WeekTemplates - .FirstOrDefault(t => id == t.Id); + var template = _weekTemplateRepository.GetWeekTemplateFromId(id); if (template == null) return NotFound(new ErrorResponse(ErrorCode.WeekTemplateNotFound, "Weektemplate not found")); @@ -249,8 +232,8 @@ public async Task DeleteTemplate(long id) if (!await _authentication.HasTemplateAccess(user, template?.DepartmentKey)) return StatusCode(StatusCodes.Status403Forbidden, new ErrorResponse(ErrorCode.NotAuthorized, "User does not have permission")); - _context.WeekTemplates.Remove(template); - await _context.SaveChangesAsync(); + _weekTemplateRepository.RemoveTemplate(template); + await _weekTemplateRepository.SaveChangesAsync(); return Ok(new SuccessResponse("Deleted week template")); } } diff --git a/GirafRepositories/WeekPlanner/Interfaces/IWeekTemplateRepository.cs b/GirafRepositories/WeekPlanner/Interfaces/IWeekTemplateRepository.cs index 274b58ff..61f563cc 100644 --- a/GirafRepositories/WeekPlanner/Interfaces/IWeekTemplateRepository.cs +++ b/GirafRepositories/WeekPlanner/Interfaces/IWeekTemplateRepository.cs @@ -1,9 +1,22 @@ +using GirafEntities.User; using GirafEntities.WeekPlanner; +using GirafEntities.WeekPlanner.DTOs; +using GirafRepositories.Interfaces; -namespace GirafRepositories.Interfaces +namespace GirafRepositories.WeekPlanner.Interfaces { public interface IWeekTemplateRepository : IRepository { - + WeekTemplateNameDTO[] GetWeekTemplatesForUser(GirafUser user); + Task GetWeekTemplateFromIdAndUser(long id, GirafUser user); + void AddWeekTemplate(WeekTemplate newTemplate); + void UpdateWeekTemplate(WeekTemplate template); + void RemoveTemplate(WeekTemplate template); + WeekTemplate GetWeekTemplateFromId(long id); + + + + + Task SaveChangesAsync(); } } \ No newline at end of file diff --git a/GirafRepositories/WeekPlanner/WeekTemplateRepository.cs b/GirafRepositories/WeekPlanner/WeekTemplateRepository.cs index 73b84e28..1d8beb62 100644 --- a/GirafRepositories/WeekPlanner/WeekTemplateRepository.cs +++ b/GirafRepositories/WeekPlanner/WeekTemplateRepository.cs @@ -1,6 +1,9 @@ +using GirafEntities.User; using GirafEntities.WeekPlanner; -using GirafRepositories.Interfaces; +using GirafEntities.WeekPlanner.DTOs; using GirafRepositories.Persistence; +using GirafRepositories.WeekPlanner.Interfaces; +using Microsoft.EntityFrameworkCore; namespace GirafRepositories.WeekPlanner { @@ -8,6 +11,60 @@ public class WeekTemplateRepository : Repository, IWeekTemplateRep { public WeekTemplateRepository(GirafDbContext context) : base(context) { + + } + + public WeekTemplateNameDTO[] GetWeekTemplatesForUser(GirafUser user) + { + return Context.WeekTemplates + .Where(t => t.DepartmentKey == user.DepartmentKey) + .Select(w => new WeekTemplateNameDTO(w.Name, w.Id)).ToArray(); + } + + public async Task GetWeekTemplateFromIdAndUser(long id, GirafUser user) + { + return await Context.WeekTemplates + .Include(w => w.Thumbnail) + .Include(u => u.Weekdays) + .ThenInclude(wd => wd.Activities) + .ThenInclude(pa => pa.Pictograms) + .ThenInclude(p => p.Pictogram) + .Where(t => t.DepartmentKey == user.DepartmentKey) + .FirstOrDefaultAsync(w => w.Id == id); + } + + public void AddWeekTemplate(WeekTemplate newTemplate) + { + Context.WeekTemplates.Add(newTemplate); + } + + public void UpdateWeekTemplate(WeekTemplate template) + { + Context.WeekTemplates.Update(template); + } + + public void RemoveTemplate(WeekTemplate template) + { + Context.WeekTemplates.Remove(template); + } + + public WeekTemplate GetWeekTemplateFromId(long id) + { + return Context.WeekTemplates + .Include(w => w.Thumbnail) + .Include(u => u.Weekdays) + .ThenInclude(wd => wd.Activities) + .ThenInclude(e => e.Pictograms) + .FirstOrDefault(t => id == t.Id); + } + + /// + /// Saves changes. + /// + /// + public Task SaveChangesAsync() + { + return Context.SaveChangesAsync(); } } }