From c131336b9047e5b73b95547ebc3114beb69562ee Mon Sep 17 00:00:00 2001 From: JMyrtue Date: Wed, 22 Nov 2023 17:28:35 +0100 Subject: [PATCH] resolves issues regarding UpdateDays method. Moves some funcitonality from repositories to service project. --- GirafEntities/WeekPlanner/Weekday.cs | 2 +- GirafRepositories/RepositoryExtensions.cs | 1 - .../WeekPlanner/Interfaces/IWeekRepository.cs | 2 - .../WeekPlanner/WeekRepository.cs | 92 ------------------- GirafServices/ServiceExtensions.cs | 1 + GirafServices/WeekPlanner/IWeekBaseService.cs | 14 +++ GirafServices/WeekPlanner/IWeekService.cs | 4 +- GirafServices/WeekPlanner/WeekBaseService.cs | 8 +- GirafServices/WeekPlanner/WeekService.cs | 33 ++++--- 9 files changed, 43 insertions(+), 114 deletions(-) create mode 100644 GirafServices/WeekPlanner/IWeekBaseService.cs diff --git a/GirafEntities/WeekPlanner/Weekday.cs b/GirafEntities/WeekPlanner/Weekday.cs index 23e12ed2..4a9c2718 100644 --- a/GirafEntities/WeekPlanner/Weekday.cs +++ b/GirafEntities/WeekPlanner/Weekday.cs @@ -42,7 +42,7 @@ public enum Days /// /// Weekday Model as concrete day for a weekplan, with activities /// - public class Weekday + public class Weekday { /// /// Primary key diff --git a/GirafRepositories/RepositoryExtensions.cs b/GirafRepositories/RepositoryExtensions.cs index bb7f7257..e652085b 100644 --- a/GirafRepositories/RepositoryExtensions.cs +++ b/GirafRepositories/RepositoryExtensions.cs @@ -37,7 +37,6 @@ public static IServiceCollection AddRepositories(this IServiceCollection service services.AddScoped(); services.AddScoped(); services.AddTransient(); - return services; } diff --git a/GirafRepositories/WeekPlanner/Interfaces/IWeekRepository.cs b/GirafRepositories/WeekPlanner/Interfaces/IWeekRepository.cs index fd6b85ce..35400d17 100644 --- a/GirafRepositories/WeekPlanner/Interfaces/IWeekRepository.cs +++ b/GirafRepositories/WeekPlanner/Interfaces/IWeekRepository.cs @@ -11,8 +11,6 @@ public interface IWeekRepository : IRepository Task LoadUserWithWeekSchedules(string userId); public Task DeleteSpecificWeek(GirafUser user, Week week); - public Task SetWeekFromDTO(WeekBaseDTO weekDTO, WeekBase week); - public Task AddPictogramsToWeekday(Weekday to, WeekdayDTO from); public Task UpdateSpecificWeek(Week week); } } \ No newline at end of file diff --git a/GirafRepositories/WeekPlanner/WeekRepository.cs b/GirafRepositories/WeekPlanner/WeekRepository.cs index 110347c3..76b250c5 100644 --- a/GirafRepositories/WeekPlanner/WeekRepository.cs +++ b/GirafRepositories/WeekPlanner/WeekRepository.cs @@ -52,98 +52,6 @@ public async Task UpdateSpecificWeek(Week week) Context.Weeks.Update(week); return await Context.SaveChangesAsync(); } - //This and AddPictogramsToWeekday should actually be seperated in the correct repositories but they were together in the same class when i moved them, and SetWeekFromDTO calls AddpictoramstoWeekday - // I do not want to instantiate a repository in a repository, or rewrite them at the moment so here they are. - /// - /// 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. - /// MissingProperties if thumbnail is missing. - /// ResourceNotFound if any pictogram id is invalid. - /// null otherwise. - /// The 2 functions where static for somereason when they where located in sharedmethods. - /// They should probably be changed to something simpler - public async Task SetWeekFromDTO(WeekBaseDTO weekDTO, WeekBase week) - { - var modelErrorCode = weekDTO.ValidateModel(); - if (modelErrorCode.HasValue) - { - return new ErrorResponse(modelErrorCode.Value, "Invalid model"); - } - - week.Name = weekDTO.Name; - - Pictogram thumbnail = Context.Pictograms - .FirstOrDefault(p => p.Id == weekDTO.Thumbnail.Id); - if (thumbnail == null) - return new ErrorResponse(ErrorCode.MissingProperties, "Missing thumbnail"); - - week.Thumbnail = thumbnail; - - foreach (var day in weekDTO.Days) - { - var wkDay = new Weekday(day); - if (!(await AddPictogramsToWeekday(wkDay, day))) - { - return new ErrorResponse(ErrorCode.ResourceNotFound, "Missing pictogram"); - } - - week.UpdateDay(wkDay); - } - - // All week days that were not specified in the new schedule, but existed before - var toBeDeleted = week.Weekdays.Where(wd => !weekDTO.Days.Any(d => d.Day == wd.Day)).ToList(); - foreach (var deletedDay in toBeDeleted) - { - week.Weekdays.Remove(deletedDay); - } - - return null; - } - - /// - /// Take pictograms and choices from DTO and add them to weekday object. - /// - /// True if all pictograms and choices were found and added, and false otherwise. - /// Pictograms and choices will be added to this object. - /// Pictograms and choices will be read from this object. - public async Task AddPictogramsToWeekday(Weekday to, WeekdayDTO from) - { - if (from.Activities != null) - { - foreach (var activityDTO in from.Activities) - { - - List pictograms = new List(); - - foreach (var pictogram in activityDTO.Pictograms) - { - var picto = await Context.Pictograms - .Where(p => p.Id == pictogram.Id).FirstOrDefaultAsync(); - - if (picto != null) - { - pictograms.Add(picto); - } - else - { - return false; - } - } - - Timer timer = null; - if (activityDTO.Timer != null) - { - timer = await Context.Timers.FirstOrDefaultAsync(t => t.Key == activityDTO.Timer.Key); - } - - if (pictograms.Any()) - to.Activities.Add(new Activity(to, pictograms, activityDTO.Order, activityDTO.State, timer, activityDTO.IsChoiceBoard, activityDTO.Title, activityDTO.ChoiceBoardName)); - } - } - return true; - } } } diff --git a/GirafServices/ServiceExtensions.cs b/GirafServices/ServiceExtensions.cs index 6d5f975e..3f95b8c7 100644 --- a/GirafServices/ServiceExtensions.cs +++ b/GirafServices/ServiceExtensions.cs @@ -14,6 +14,7 @@ public static IServiceCollection AddServices(this IServiceCollection services) services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); return services; } } diff --git a/GirafServices/WeekPlanner/IWeekBaseService.cs b/GirafServices/WeekPlanner/IWeekBaseService.cs new file mode 100644 index 00000000..6cec65bc --- /dev/null +++ b/GirafServices/WeekPlanner/IWeekBaseService.cs @@ -0,0 +1,14 @@ +using GirafEntities.WeekPlanner; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GirafServices.WeekPlanner +{ + public interface IWeekBaseService + { + void UpdateDay(Weekday day, WeekBase wb); + } +} diff --git a/GirafServices/WeekPlanner/IWeekService.cs b/GirafServices/WeekPlanner/IWeekService.cs index 16160e69..ba8913a2 100644 --- a/GirafServices/WeekPlanner/IWeekService.cs +++ b/GirafServices/WeekPlanner/IWeekService.cs @@ -7,8 +7,8 @@ namespace GirafServices.WeekPlanner { public interface IWeekService { - public Task SetWeekFromDTO(WeekBaseDTO weekDTO, WeekBase week, IGirafService _giraf); - public Task AddPictogramsToWeekday(Weekday to, WeekdayDTO from, IGirafService _giraf); + public Task SetWeekFromDTO(WeekBaseDTO weekDTO, WeekBase week); + public Task AddPictogramsToWeekday(Weekday to, WeekdayDTO from); } diff --git a/GirafServices/WeekPlanner/WeekBaseService.cs b/GirafServices/WeekPlanner/WeekBaseService.cs index cbabff64..7e7d0849 100644 --- a/GirafServices/WeekPlanner/WeekBaseService.cs +++ b/GirafServices/WeekPlanner/WeekBaseService.cs @@ -2,17 +2,17 @@ namespace GirafServices.WeekPlanner { - public class WeekBaseService + public class WeekBaseService : IWeekBaseService { /// /// Updates the given weekday of the Week with the new information found in 'day'. /// /// A day instance to update the week with - the old one is completely overridden. - public void UpdateDay(Weekday day) + public void UpdateDay(Weekday day, WeekBase wb) { - var wd = Weekdays.FirstOrDefault(d => d.Day == day.Day); + var wd = wb.Weekdays.FirstOrDefault(d => d.Day == day.Day); if (wd == null) - Weekdays.Add(day); + wb.Weekdays.Add(day); else wd.Activities = day.Activities; } diff --git a/GirafServices/WeekPlanner/WeekService.cs b/GirafServices/WeekPlanner/WeekService.cs index 5034feb7..fc29f8e7 100644 --- a/GirafServices/WeekPlanner/WeekService.cs +++ b/GirafServices/WeekPlanner/WeekService.cs @@ -4,6 +4,7 @@ using Timer = GirafEntities.WeekPlanner.Timer; using GirafEntities.Responses; using GirafEntities.WeekPlanner.DTOs; +using GirafRepositories.Interfaces; namespace GirafServices.WeekPlanner { @@ -18,7 +19,19 @@ namespace GirafServices.WeekPlanner /// null otherwise. public class WeekService : IWeekService { - public async Task SetWeekFromDTO(WeekBaseDTO weekDTO, WeekBase week, IGirafService _giraf) + private readonly IWeekBaseService _weekBaseService; + private readonly IPictogramRepository _pictogramRepository; + private readonly ITimerRepository _timerRepository; + + + public WeekService(IWeekBaseService weekBaseService, IPictogramRepository pictogramRepository, ITimerRepository timerRepository) + { + _weekBaseService = weekBaseService; + _pictogramRepository = pictogramRepository; + _timerRepository = timerRepository; + } + + public async Task SetWeekFromDTO(WeekBaseDTO weekDTO, WeekBase week) { var modelErrorCode = weekDTO.ValidateModel(); if (modelErrorCode.HasValue) @@ -28,8 +41,7 @@ public async Task SetWeekFromDTO(WeekBaseDTO weekDTO, WeekBase we week.Name = weekDTO.Name; - Pictogram thumbnail = _giraf._context.Pictograms - .FirstOrDefault(p => p.Id == weekDTO.Thumbnail.Id); + Pictogram thumbnail = await _pictogramRepository.GetPictogramsById(weekDTO.Thumbnail.Id); if (thumbnail == null) return new ErrorResponse(ErrorCode.MissingProperties, "Missing thumbnail"); @@ -38,12 +50,12 @@ public async Task SetWeekFromDTO(WeekBaseDTO weekDTO, WeekBase we foreach (var day in weekDTO.Days) { var wkDay = new Weekday(day); - if (!(await AddPictogramsToWeekday(wkDay, day, _giraf))) + if (!(await AddPictogramsToWeekday(wkDay, day))) { return new ErrorResponse(ErrorCode.ResourceNotFound, "Missing pictogram"); } - week.UpdateDay(wkDay); + _weekBaseService.UpdateDay(wkDay, week); } // All week days that were not specified in the new schedule, but existed before @@ -62,8 +74,7 @@ public async Task SetWeekFromDTO(WeekBaseDTO weekDTO, WeekBase we /// True if all pictograms and choices were found and added, and false otherwise. /// Pictograms and choices will be added to this object. /// Pictograms and choices will be read from this object. - /// IGirafService for injection. - public async Task AddPictogramsToWeekday(Weekday to, WeekdayDTO from, IGirafService _giraf) + public async Task AddPictogramsToWeekday(Weekday to, WeekdayDTO from) { if (from.Activities != null) { @@ -74,8 +85,7 @@ public async Task AddPictogramsToWeekday(Weekday to, WeekdayDTO from, IGir foreach (var pictogram in activityDTO.Pictograms) { - var picto = await _giraf._context.Pictograms - .Where(p => p.Id == pictogram.Id).FirstOrDefaultAsync(); + var picto = await _pictogramRepository.GetPictogramsById(pictogram.Id); if (picto != null) { @@ -90,12 +100,11 @@ public async Task AddPictogramsToWeekday(Weekday to, WeekdayDTO from, IGir Timer timer = null; if (activityDTO.Timer != null) { - timer = await _giraf._context.Timers.Where(t => t.Key == activityDTO.Timer.Key).FirstOrDefaultAsync(); + timer = await _timerRepository.getTimerWithKey(activityDTO.Timer.Key); } if (pictograms.Any()) - to.Activities.Add(new Activity(to, pictograms, activityDTO.Order, activityDTO.State, timer, - activityDTO.IsChoiceBoard, activityDTO.Title, activityDTO.ChoiceBoardName)); + to.Activities.Add(new Activity(to, pictograms, activityDTO.Order, activityDTO.State, timer, activityDTO.IsChoiceBoard, activityDTO.Title, activityDTO.ChoiceBoardName)); } } return true;