Skip to content

Commit

Permalink
resolves issues regarding UpdateDays method. Moves some funcitonality…
Browse files Browse the repository at this point in the history
… from repositories to service project.
  • Loading branch information
JMyrtue committed Nov 22, 2023
1 parent 6211672 commit c131336
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 114 deletions.
2 changes: 1 addition & 1 deletion GirafEntities/WeekPlanner/Weekday.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public enum Days
/// <summary>
/// Weekday Model as concrete day for a weekplan, with activities
/// </summary>
public class Weekday
public class Weekday
{
/// <summary>
/// Primary key
Expand Down
1 change: 0 additions & 1 deletion GirafRepositories/RepositoryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public static IServiceCollection AddRepositories(this IServiceCollection service
services.AddScoped<IImageRepository, ImageRepository>();
services.AddScoped<IWeekdayRepository, WeekdayRepository>();
services.AddTransient<IActivityRepository, ActivityRepository>();

return services;
}

Expand Down
2 changes: 0 additions & 2 deletions GirafRepositories/WeekPlanner/Interfaces/IWeekRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ public interface IWeekRepository : IRepository<Week>

Task<GirafUser> LoadUserWithWeekSchedules(string userId);
public Task<int> DeleteSpecificWeek(GirafUser user, Week week);
public Task<ErrorResponse> SetWeekFromDTO(WeekBaseDTO weekDTO, WeekBase week);
public Task<bool> AddPictogramsToWeekday(Weekday to, WeekdayDTO from);
public Task<int> UpdateSpecificWeek(Week week);
}
}
92 changes: 0 additions & 92 deletions GirafRepositories/WeekPlanner/WeekRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,98 +52,6 @@ public async Task<int> 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.
/// <summary>
/// From the given DTO, set the name, thumbnail and days of the given week object.
/// </summary>
/// <param name="weekDTO">The DTO from which values are read.</param>
/// <param name="week">The week object to which values are written.</param>
/// <returns>MissingProperties if thumbnail is missing.
/// ResourceNotFound if any pictogram id is invalid.
/// null otherwise.</returns>
/// The 2 functions where static for somereason when they where located in sharedmethods.
/// They should probably be changed to something simpler
public async Task<ErrorResponse> 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;
}

/// <summary>
/// Take pictograms and choices from DTO and add them to weekday object.
/// </summary>
/// <returns>True if all pictograms and choices were found and added, and false otherwise.</returns>
/// <param name="to">Pictograms and choices will be added to this object.</param>
/// <param name="from">Pictograms and choices will be read from this object.</param>
public async Task<bool> AddPictogramsToWeekday(Weekday to, WeekdayDTO from)
{
if (from.Activities != null)
{
foreach (var activityDTO in from.Activities)
{

List<Pictogram> pictograms = new List<Pictogram>();

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;
}
}
}

1 change: 1 addition & 0 deletions GirafServices/ServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public static IServiceCollection AddServices(this IServiceCollection services)
services.AddTransient<IGirafService, GirafService>();
services.AddTransient<IAuthenticationService, GirafAuthenticationService>();
services.AddTransient<IWeekService, WeekService>();
services.AddTransient<IWeekBaseService, WeekBaseService>();
return services;
}
}
Expand Down
14 changes: 14 additions & 0 deletions GirafServices/WeekPlanner/IWeekBaseService.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
4 changes: 2 additions & 2 deletions GirafServices/WeekPlanner/IWeekService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace GirafServices.WeekPlanner
{
public interface IWeekService
{
public Task<ErrorResponse> SetWeekFromDTO(WeekBaseDTO weekDTO, WeekBase week, IGirafService _giraf);
public Task<bool> AddPictogramsToWeekday(Weekday to, WeekdayDTO from, IGirafService _giraf);
public Task<ErrorResponse> SetWeekFromDTO(WeekBaseDTO weekDTO, WeekBase week);
public Task<bool> AddPictogramsToWeekday(Weekday to, WeekdayDTO from);


}
Expand Down
8 changes: 4 additions & 4 deletions GirafServices/WeekPlanner/WeekBaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

namespace GirafServices.WeekPlanner
{
public class WeekBaseService
public class WeekBaseService : IWeekBaseService
{
/// <summary>
/// Updates the given weekday of the Week with the new information found in 'day'.
/// </summary>
/// <param name="day">A day instance to update the week with - the old one is completely overridden.</param>
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;
}
Expand Down
33 changes: 21 additions & 12 deletions GirafServices/WeekPlanner/WeekService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Timer = GirafEntities.WeekPlanner.Timer;
using GirafEntities.Responses;
using GirafEntities.WeekPlanner.DTOs;
using GirafRepositories.Interfaces;

namespace GirafServices.WeekPlanner
{
Expand All @@ -18,7 +19,19 @@ namespace GirafServices.WeekPlanner
/// null otherwise.</returns>
public class WeekService : IWeekService
{
public async Task<ErrorResponse> 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<ErrorResponse> SetWeekFromDTO(WeekBaseDTO weekDTO, WeekBase week)
{
var modelErrorCode = weekDTO.ValidateModel();
if (modelErrorCode.HasValue)
Expand All @@ -28,8 +41,7 @@ public async Task<ErrorResponse> 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");

Expand All @@ -38,12 +50,12 @@ public async Task<ErrorResponse> 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
Expand All @@ -62,8 +74,7 @@ public async Task<ErrorResponse> SetWeekFromDTO(WeekBaseDTO weekDTO, WeekBase we
/// <returns>True if all pictograms and choices were found and added, and false otherwise.</returns>
/// <param name="to">Pictograms and choices will be added to this object.</param>
/// <param name="from">Pictograms and choices will be read from this object.</param>
/// <param name="_giraf">IGirafService for injection.</param>
public async Task<bool> AddPictogramsToWeekday(Weekday to, WeekdayDTO from, IGirafService _giraf)
public async Task<bool> AddPictogramsToWeekday(Weekday to, WeekdayDTO from)
{
if (from.Activities != null)
{
Expand All @@ -74,8 +85,7 @@ public async Task<bool> 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)
{
Expand All @@ -90,12 +100,11 @@ public async Task<bool> 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;
Expand Down

0 comments on commit c131336

Please sign in to comment.