diff --git a/GirafAPI/Controllers/WeekTemplateController.cs b/GirafAPI/Controllers/WeekTemplateController.cs index 0270cb86..dd35b8df 100644 --- a/GirafAPI/Controllers/WeekTemplateController.cs +++ b/GirafAPI/Controllers/WeekTemplateController.cs @@ -4,6 +4,7 @@ using GirafRest.Models; using GirafRest.Models.DTOs; using GirafRest.Models.Responses; +using GirafServices.WeekPlanner; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -22,6 +23,7 @@ namespace GirafAPI.Controllers [Route("v1/[controller]")] public class WeekTemplateController : Controller { + private readonly IWeekService _weekService; /// /// A reference to GirafService, that contains common functionality for all controllers. /// @@ -45,12 +47,14 @@ public class WeekTemplateController : Controller public WeekTemplateController(IGirafService giraf, ILoggerFactory loggerFactory, IAuthenticationService authentication, - GirafDbContext context) + GirafDbContext context, + IWeekService weekService) { _giraf = giraf; _giraf._logger = loggerFactory.CreateLogger("WeekTemplate"); _authentication = authentication; _context = context; + _weekService = weekService; } /// @@ -150,7 +154,7 @@ public async Task CreateWeekTemplate([FromBody] WeekTemplateDTO te var newTemplate = new WeekTemplate(department); - var errorCode = await SetWeekFromDTO(templateDto, newTemplate, _giraf); + var errorCode = await _weekService.SetWeekFromDTO(templateDto, newTemplate, _giraf); if (errorCode != null) return BadRequest(errorCode); @@ -203,7 +207,7 @@ public async Task UpdateWeekTemplate(long id, [FromBody] WeekTempl if (!await _authentication.HasTemplateAccess(user, template?.DepartmentKey)) return StatusCode(StatusCodes.Status403Forbidden, new ErrorResponse(ErrorCode.NotAuthorized, "User does not have permission")); - var errorCode = await SetWeekFromDTO(newValuesDto, template, _giraf); + var errorCode = await _weekService.SetWeekFromDTO(newValuesDto, template, _giraf); if (errorCode != null) return BadRequest(errorCode); diff --git a/GirafEntities/WeekPlanner/DTOs/PictogramDTO.cs b/GirafEntities/WeekPlanner/DTOs/PictogramDTO.cs index 671c0ea3..64b33d58 100644 --- a/GirafEntities/WeekPlanner/DTOs/PictogramDTO.cs +++ b/GirafEntities/WeekPlanner/DTOs/PictogramDTO.cs @@ -11,12 +11,12 @@ public class PictogramDTO /// /// Primary Key /// - public long Id { get; internal set; } + public long Id { get; set; } /// /// The last time the pictogram was edited. /// - public DateTime LastEdit { get; internal set; } + public DateTime LastEdit { get; set; } /// /// The title of the pictogram. diff --git a/GirafEntities/WeekPlanner/DTOs/WeekDTO.cs b/GirafEntities/WeekPlanner/DTOs/WeekDTO.cs index e6988142..88a28245 100644 --- a/GirafEntities/WeekPlanner/DTOs/WeekDTO.cs +++ b/GirafEntities/WeekPlanner/DTOs/WeekDTO.cs @@ -8,12 +8,12 @@ public class WeekDTO : WeekBaseDTO /// /// The year of the week. /// - public int WeekYear { get; internal set; } + public int WeekYear { get; set; } /// /// The number of the week, 0 - 52 (53). /// - public int WeekNumber { get; internal set; } + public int WeekNumber { get; set; } /// /// Empty contstructor used by JSON Generation diff --git a/GirafServices/User/IGirafService.cs b/GirafServices/User/IGirafService.cs index 7180c339..ceb29203 100644 --- a/GirafServices/User/IGirafService.cs +++ b/GirafServices/User/IGirafService.cs @@ -1,10 +1,7 @@ -using GirafRest.Data; -using GirafRest.Models; +using GirafRest.Models; using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.Logging; -using System.IO; using System.Security.Claims; -using System.Threading.Tasks; using GirafEntities.User; using GirafRepositories.Persistence; diff --git a/GirafServices/WeekPlanner/IWeekService.cs b/GirafServices/WeekPlanner/IWeekService.cs new file mode 100644 index 00000000..add8eff0 --- /dev/null +++ b/GirafServices/WeekPlanner/IWeekService.cs @@ -0,0 +1,20 @@ +using GirafRest.Interfaces; +using GirafRest.Models.DTOs; +using GirafRest.Models.Responses; +using GirafRest.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GirafServices.WeekPlanner +{ + public interface IWeekService + { + public Task SetWeekFromDTO(WeekBaseDTO weekDTO, WeekBase week, IGirafService _giraf); + public Task AddPictogramsToWeekday(Weekday to, WeekdayDTO from, IGirafService _giraf); + + + } +} diff --git a/GirafServices/SharedMethods.cs b/GirafServices/WeekPlanner/WeekService.cs similarity index 74% rename from GirafServices/SharedMethods.cs rename to GirafServices/WeekPlanner/WeekService.cs index 6c078ebf..213abd5b 100644 --- a/GirafServices/SharedMethods.cs +++ b/GirafServices/WeekPlanner/WeekService.cs @@ -1,39 +1,39 @@ -using GirafRest.Models; +using GirafEntities.WeekPlanner; +using GirafRest.Interfaces; +using GirafRest.Models; using GirafRest.Models.DTOs; using GirafRest.Models.Responses; -using GirafRest.Interfaces; using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; using System.Linq; +using System.Text; using System.Threading.Tasks; -using System.Collections.Generic; -using GirafEntities.WeekPlanner; +using Timer = GirafEntities.WeekPlanner.Timer; -namespace GirafRest.Shared +namespace GirafServices.WeekPlanner { /// - /// Shared static class for helper methods + /// From the given DTO, set the name, thumbnail and days of the given week object. /// - public static class SharedMethods + /// The DTO from which values are read. + /// The week object to which values are written. + /// An instance of the GirafService from which the database will be accessed when reading the DTO. + /// MissingProperties if thumbnail is missing. + /// ResourceNotFound if any pictogram id is invalid. + /// null otherwise. + public class WeekService : IWeekService { - /// - /// From the given DTO, set the name, thumbnail and days of the given week object. - /// - /// The DTO from which values are read. - /// The week object to which values are written. - /// An instance of the GirafService from which the database will be accessed when reading the DTO. - /// MissingProperties if thumbnail is missing. - /// ResourceNotFound if any pictogram id is invalid. - /// null otherwise. - public static async Task SetWeekFromDTO(WeekBaseDTO weekDTO, WeekBase week, IGirafService _giraf) + public async Task SetWeekFromDTO(WeekBaseDTO weekDTO, WeekBase week, IGirafService _giraf) { var modelErrorCode = weekDTO.ValidateModel(); if (modelErrorCode.HasValue) { return new ErrorResponse(modelErrorCode.Value, "Invalid model"); } - + week.Name = weekDTO.Name; - + Pictogram thumbnail = _giraf._context.Pictograms .FirstOrDefault(p => p.Id == weekDTO.Thumbnail.Id); if (thumbnail == null) @@ -69,13 +69,13 @@ public static async Task SetWeekFromDTO(WeekBaseDTO weekDTO, Week /// Pictograms and choices will be added to this object. /// Pictograms and choices will be read from this object. /// IGirafService for injection. - public static async Task AddPictogramsToWeekday(Weekday to, WeekdayDTO from, IGirafService _giraf) + public async Task AddPictogramsToWeekday(Weekday to, WeekdayDTO from, IGirafService _giraf) { if (from.Activities != null) { foreach (var activityDTO in from.Activities) { - + List pictograms = new List(); foreach (var pictogram in activityDTO.Pictograms) @@ -94,11 +94,11 @@ public static async Task AddPictogramsToWeekday(Weekday to, WeekdayDTO fro } Timer timer = null; - if(activityDTO.Timer != null) + if (activityDTO.Timer != null) { timer = await _giraf._context.Timers.Where(t => t.Key == activityDTO.Timer.Key).FirstOrDefaultAsync(); } - + if (pictograms.Any()) to.Activities.Add(new Activity(to, pictograms, activityDTO.Order, activityDTO.State, timer, activityDTO.IsChoiceBoard, activityDTO.Title, activityDTO.ChoiceBoardName)); @@ -107,4 +107,4 @@ public static async Task AddPictogramsToWeekday(Weekday to, WeekdayDTO fro return true; } } -} \ No newline at end of file +}