Skip to content

Commit

Permalink
remove all use of girafdbcontext in week template controller + setup …
Browse files Browse the repository at this point in the history
…weektemplate repository
  • Loading branch information
andreaswurtz committed Nov 22, 2023
1 parent 72b4bcc commit 8bd68fb
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 37 deletions.
51 changes: 17 additions & 34 deletions GirafAPI/Controllers/WeekTemplateController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -36,10 +35,8 @@ public class WeekTemplateController : Controller
/// </summary>
private readonly IAuthenticationService _authentication;

// SHOULD BE REMOVED AFTER REFACTORING OF THIS CONTROLLER HAS BEEN COMPLETED!
private readonly GirafDbContext _context;


private readonly IWeekTemplateRepository _weekTemplateRepository;

/// <summary>
/// Constructor is called by the asp.net runtime.
/// </summary>
Expand All @@ -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;
}
Expand All @@ -79,9 +77,7 @@ public async Task<ActionResult> 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)
{
Expand Down Expand Up @@ -109,14 +105,7 @@ public async Task<ActionResult> 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"));
Expand Down Expand Up @@ -162,8 +151,8 @@ public async Task<ActionResult> 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 },
Expand Down Expand Up @@ -198,12 +187,7 @@ public async Task<ActionResult> 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"));
Expand All @@ -215,8 +199,8 @@ public async Task<ActionResult> 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<WeekTemplateDTO>(new WeekTemplateDTO(template)));
}

Expand All @@ -240,17 +224,16 @@ public async Task<ActionResult> 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"));

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"));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<WeekTemplate>
{

WeekTemplateNameDTO[] GetWeekTemplatesForUser(GirafUser user);
Task<WeekTemplate> GetWeekTemplateFromIdAndUser(long id, GirafUser user);
void AddWeekTemplate(WeekTemplate newTemplate);
void UpdateWeekTemplate(WeekTemplate template);
void RemoveTemplate(WeekTemplate template);
WeekTemplate GetWeekTemplateFromId(long id);




Task<int> SaveChangesAsync();
}
}
59 changes: 58 additions & 1 deletion GirafRepositories/WeekPlanner/WeekTemplateRepository.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,70 @@
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
{
public class WeekTemplateRepository : Repository<WeekTemplate>, IWeekTemplateRepository
{
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<WeekTemplate> 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);
}

/// <summary>
/// Saves changes.
/// </summary>
/// <returns></returns>
public Task<int> SaveChangesAsync()
{
return Context.SaveChangesAsync();
}
}
}

0 comments on commit 8bd68fb

Please sign in to comment.