Skip to content

Commit

Permalink
removes sharedMethods from service, by creating weekService, and depe…
Browse files Browse the repository at this point in the history
…ndency injecting it into a controller.
  • Loading branch information
JMyrtue committed Nov 22, 2023
1 parent e9c3fb7 commit e63682c
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 35 deletions.
10 changes: 7 additions & 3 deletions GirafAPI/Controllers/WeekTemplateController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,6 +23,7 @@ namespace GirafAPI.Controllers
[Route("v1/[controller]")]
public class WeekTemplateController : Controller
{
private readonly IWeekService _weekService;
/// <summary>
/// A reference to GirafService, that contains common functionality for all controllers.
/// </summary>
Expand All @@ -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;
}

/// <summary>
Expand Down Expand Up @@ -150,7 +154,7 @@ public async Task<ActionResult> 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);

Expand Down Expand Up @@ -203,7 +207,7 @@ public async Task<ActionResult> 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);

Expand Down
4 changes: 2 additions & 2 deletions GirafEntities/WeekPlanner/DTOs/PictogramDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public class PictogramDTO
/// <summary>
/// Primary Key
/// </summary>
public long Id { get; internal set; }
public long Id { get; set; }

/// <summary>
/// The last time the pictogram was edited.
/// </summary>
public DateTime LastEdit { get; internal set; }
public DateTime LastEdit { get; set; }

/// <summary>
/// The title of the pictogram.
Expand Down
4 changes: 2 additions & 2 deletions GirafEntities/WeekPlanner/DTOs/WeekDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ public class WeekDTO : WeekBaseDTO
/// <summary>
/// The year of the week.
/// </summary>
public int WeekYear { get; internal set; }
public int WeekYear { get; set; }

/// <summary>
/// The number of the week, 0 - 52 (53).
/// </summary>
public int WeekNumber { get; internal set; }
public int WeekNumber { get; set; }

/// <summary>
/// Empty contstructor used by JSON Generation
Expand Down
5 changes: 1 addition & 4 deletions GirafServices/User/IGirafService.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
20 changes: 20 additions & 0 deletions GirafServices/WeekPlanner/IWeekService.cs
Original file line number Diff line number Diff line change
@@ -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<ErrorResponse> SetWeekFromDTO(WeekBaseDTO weekDTO, WeekBase week, IGirafService _giraf);
public Task<bool> AddPictogramsToWeekday(Weekday to, WeekdayDTO from, IGirafService _giraf);


}
}
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Shared static class for helper methods
/// From the given DTO, set the name, thumbnail and days of the given week object.
/// </summary>
public static class SharedMethods
/// <param name="weekDTO">The DTO from which values are read.</param>
/// <param name="week">The week object to which values are written.</param>
/// <param name="_giraf">An instance of the GirafService from which the database will be accessed when reading the DTO.</param>
/// <returns>MissingProperties if thumbnail is missing.
/// ResourceNotFound if any pictogram id is invalid.
/// null otherwise.</returns>
public class WeekService : IWeekService
{
/// <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>
/// <param name="_giraf">An instance of the GirafService from which the database will be accessed when reading the DTO.</param>
/// <returns>MissingProperties if thumbnail is missing.
/// ResourceNotFound if any pictogram id is invalid.
/// null otherwise.</returns>
public static async Task<ErrorResponse> SetWeekFromDTO(WeekBaseDTO weekDTO, WeekBase week, IGirafService _giraf)
public async Task<ErrorResponse> 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)
Expand Down Expand Up @@ -69,13 +69,13 @@ public static async Task<ErrorResponse> SetWeekFromDTO(WeekBaseDTO weekDTO, Week
/// <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 static async Task<bool> AddPictogramsToWeekday(Weekday to, WeekdayDTO from, IGirafService _giraf)
public async Task<bool> AddPictogramsToWeekday(Weekday to, WeekdayDTO from, IGirafService _giraf)
{
if (from.Activities != null)
{
foreach (var activityDTO in from.Activities)
{

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

foreach (var pictogram in activityDTO.Pictograms)
Expand All @@ -94,11 +94,11 @@ public static async Task<bool> 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));
Expand All @@ -107,4 +107,4 @@ public static async Task<bool> AddPictogramsToWeekday(Weekday to, WeekdayDTO fro
return true;
}
}
}
}

0 comments on commit e63682c

Please sign in to comment.