Skip to content

Commit

Permalink
Remove direct access to db Department table through weektemplate cont…
Browse files Browse the repository at this point in the history
…roller
  • Loading branch information
andreaswurtz committed Nov 22, 2023
1 parent e7e1b6a commit 72b4bcc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
9 changes: 6 additions & 3 deletions GirafAPI/Controllers/WeekTemplateController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -25,6 +24,8 @@ namespace GirafAPI.Controllers
public class WeekTemplateController : Controller
{
private readonly IWeekService _weekService;
private readonly IDepartmentService _departmentService;

/// <summary>
/// A reference to GirafService, that contains common functionality for all controllers.
/// </summary>
Expand All @@ -49,13 +50,15 @@ public WeekTemplateController(IGirafService giraf,
ILoggerFactory loggerFactory,
IAuthenticationService authentication,
GirafDbContext context,
IWeekService weekService)
IWeekService weekService,
IDepartmentService departmentService)
{
_giraf = giraf;
_giraf._logger = loggerFactory.CreateLogger("WeekTemplate");
_authentication = authentication;
_context = context;
_weekService = weekService;
_departmentService = departmentService;
}

/// <summary>
Expand Down Expand Up @@ -149,7 +152,7 @@ public async Task<ActionResult> CreateWeekTemplate([FromBody] WeekTemplateDTO te
if (templateDto == null)
return BadRequest(new ErrorResponse(ErrorCode.MissingProperties, "Missing templateDto"));

Department department = _context.Departments.FirstOrDefault(d => d.Key == user.DepartmentKey);
Department department = await _departmentService.GetDepartmentByUser(user);
if (department == null)
return BadRequest(new ErrorResponse(ErrorCode.UserMustBeInDepartment, "User must be in a department"));

Expand Down
5 changes: 5 additions & 0 deletions GirafRepositories/User/DepartmentRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public DepartmentRepository(GirafDbContext context) : base(context)
{
}

public Department GetDepartmentByUser(GirafUser user)
{
return Context.Departments.FirstOrDefault(d => d.Key == user.DepartmentKey);
}

public Department GetDepartmentById(long departmentId)
{
return Context.Departments.FirstOrDefault(dep => dep.Key == departmentId);
Expand Down
2 changes: 1 addition & 1 deletion GirafRepositories/User/Interfaces/IDepartmentRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace GirafRepositories.Interfaces
public interface IDepartmentRepository : IRepository<Department>
{
Department GetDepartmentById(long departmentId);

Department GetDepartmentByUser(GirafUser user);
Task<List<DepartmentNameDTO>> GetDepartmentNames();

Task<Department> GetDepartmentMembers(long departmentId);
Expand Down
19 changes: 19 additions & 0 deletions GirafServices/User/DepartmentService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using GirafEntities.User;
using GirafRepositories.Interfaces;

namespace GirafServices.User;

public class DepartmentService : IDepartmentService
{
private readonly IDepartmentRepository _departmentRepository;

public DepartmentService(IDepartmentRepository departmentRepository)
{
_departmentRepository = departmentRepository;
}

public async Task<Department> GetDepartmentByUser(GirafUser user)
{
return _departmentRepository.GetDepartmentByUser(user);
}
}
8 changes: 8 additions & 0 deletions GirafServices/User/IDepartmentService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using GirafEntities.User;

namespace GirafServices.User;

public interface IDepartmentService
{
Task<Department> GetDepartmentByUser(GirafUser user);
}

0 comments on commit 72b4bcc

Please sign in to comment.