Skip to content

Commit

Permalink
Di register (#357)
Browse files Browse the repository at this point in the history
* Added repository DI to the RepositoryExtension

* Adding missing service DI, and commenting on further improvements

* resolves issues regarding UpdateDays method. Moves some funcitonality from repositories to service project. (#355)

* Department entities giraf service refactor (#356)

* removed methods from department DTO into service

* removed dbcontext from service layer in GirafService, now renamed UserService

* renamed girafService dependency in AccountController

---------

Co-authored-by: Schoogle <[email protected]>

* fixes minor issues after merge

---------

Co-authored-by: Kristian <[email protected]>
Co-authored-by: Schoogle <[email protected]>
  • Loading branch information
3 people authored Nov 24, 2023
1 parent e7e1b6a commit 6e7a8ea
Show file tree
Hide file tree
Showing 25 changed files with 215 additions and 274 deletions.
18 changes: 9 additions & 9 deletions GirafAPI/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace GirafAPI.Controllers
public class AccountController : Controller
{
private readonly SignInManager<GirafUser> _signInManager;
private readonly IGirafService _giraf;
private readonly IUserService _userService;
private readonly IOptions<JwtConfig> _configuration;
private readonly IAuthenticationService _authentication;
private readonly IGirafUserRepository _userRepository;
Expand All @@ -45,7 +45,7 @@ public class AccountController : Controller
/// </summary>
/// <param name="signInManager">Service Injection</param>
/// <param name="loggerFactory">Service Injection</param>
/// <param name="giraf">Service Injection</param>
/// <param name="userService">Service Injection</param>
/// <param name="configuration">Service Injection</param>
/// <param name="userRepository">Service Injection</param>
/// <param name="departmentRepository">Service Injection</param>
Expand All @@ -54,16 +54,16 @@ public class AccountController : Controller
public AccountController(
SignInManager<GirafUser> signInManager,
ILoggerFactory loggerFactory,
IGirafService giraf,
IUserService userService,
IOptions<JwtConfig> configuration,
IGirafUserRepository userRepository,
IDepartmentRepository departmentRepository,
IGirafRoleRepository girafRoleRepository,
ISettingRepository settingRepository)
{
_signInManager = signInManager;
_giraf = giraf;
_giraf._logger = loggerFactory.CreateLogger("Account");
_userService = userService;
_userService._logger = loggerFactory.CreateLogger("Account");
_configuration = configuration;
_userRepository = userRepository;
_departmentRepository = departmentRepository;
Expand Down Expand Up @@ -250,12 +250,12 @@ public async Task<ActionResult> ChangePasswordByToken(string userId, [FromBody]
if (model.Token == null || model.Password == null)
return BadRequest(new ErrorResponse(ErrorCode.MissingProperties, "Missing token or password"));

var result = await _giraf._userManager.ResetPasswordAsync(user, model.Token, model.Password);
var result = await _userService._userManager.ResetPasswordAsync(user, model.Token, model.Password);
if (!result.Succeeded)
return Unauthorized(new ErrorResponse(ErrorCode.InvalidProperties, "Invalid token"));

await _signInManager.SignInAsync(user, isPersistent: false);
_giraf._logger.LogInformation("User changed their password successfully.");
_userService._logger.LogInformation("User changed their password successfully.");
return Ok(new SuccessResponse("User password changed succesfully"));
}

Expand All @@ -279,7 +279,7 @@ public async Task<ActionResult> GetPasswordResetToken(string userId)
if (user == null)
return NotFound(new ErrorResponse(ErrorCode.UserNotFound, "User not found"));

var result = await _giraf._userManager.GeneratePasswordResetTokenAsync(user);
var result = await _userService._userManager.GeneratePasswordResetTokenAsync(user);
return Ok(new SuccessResponse(result));
}

Expand Down Expand Up @@ -321,7 +321,7 @@ public async Task<ActionResult> DeleteUser(string userId)
private async Task<List<Claim>> GetRoleClaims(GirafUser user)
{
var roleclaims = new List<Claim>();
var userRoles = await _giraf._userManager.GetRolesAsync(user);
var userRoles = await _userService._userManager.GetRolesAsync(user);
roleclaims.AddRange(userRoles.Select(userRole => new Claim(ClaimTypes.Role, userRole)));
return roleclaims;
}
Expand Down
4 changes: 2 additions & 2 deletions GirafAPI/Controllers/AlternateNameController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace GirafAPI.Controllers
[Route("v2/[Controller]")]
public class AlternateNameController : Controller
{
private readonly IGirafService _giraf;
private readonly IUserService _giraf;
private readonly IAuthenticationService _authentication;
private readonly IPictogramRepository _pictogramRepository;
private readonly IGirafUserRepository _girafUserRepository;
Expand All @@ -34,7 +34,7 @@ public class AlternateNameController : Controller
/// <param name="girafUserRepository">The <see cref="IGirafUserRepository"/> used to query Users</param>
/// <param name="alternateNameRepository">The <see cref="IAlternateNameRepository"/> used to query alternate names</param>
public AlternateNameController(
IGirafService girafService,
IUserService girafService,
ILoggerFactory lFactory,
IPictogramRepository pictogramRepository,
IGirafUserRepository girafUserRepository,
Expand Down
14 changes: 8 additions & 6 deletions GirafAPI/Controllers/DepartmentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ namespace GirafAPI.Controllers
[Route("v1/[controller]")]
public class DepartmentController : Controller
{
private readonly IGirafService _giraf;
private readonly IUserService _giraf;

public readonly RoleManager<GirafRole> _roleManager;
public DepartmentDTO _departmentdto { get; set; } = new DepartmentDTO();
private readonly IDepartmentRepository _departmentRepository;
private readonly IGirafUserRepository _userRepository;
private readonly IGirafRoleRepository _roleRepository;
private readonly IPictogramRepository _pictogramRepository;
private readonly IUserService _girafUserService;

/// <summary>
/// Initializes new DepartmentController, injecting services
Expand All @@ -40,13 +40,14 @@ public class DepartmentController : Controller
/// <param name="departmentRepository">Department Injection</param>
/// <param name="girafRoleRepository">GIRAF Role Injection</param>
/// <param name="pictogramRepository">Pictogram Injection</param>
public DepartmentController(IGirafService giraf,
public DepartmentController(IUserService giraf,
ILoggerFactory loggerFactory,
RoleManager<GirafRole> roleManager,
IGirafUserRepository userRepository,
IDepartmentRepository departmentRepository,
IGirafRoleRepository girafRoleRepository,
IPictogramRepository pictogramRepository)
IPictogramRepository pictogramRepository,
IUserService girafUserService)
{
_giraf = giraf;
_giraf._logger = loggerFactory.CreateLogger("Department");
Expand All @@ -55,6 +56,7 @@ public DepartmentController(IGirafService giraf,
_departmentRepository = departmentRepository;
_roleRepository = girafRoleRepository;
_pictogramRepository = pictogramRepository;
_girafUserService = girafUserService;
}

/// <summary>
Expand Down Expand Up @@ -108,7 +110,7 @@ public async Task<ActionResult> Get(long id)
return this.ResourceNotFound(nameof(Department));
}

var members = _departmentdto.FindMembers(department.Result.Members, _roleManager, _giraf);
var members = _girafUserService.FindMembers(department.Result.Members, _roleManager, _giraf);
return Ok(new SuccessResponse<DepartmentDTO>(new DepartmentDTO(department.Result, members)));
}

Expand Down Expand Up @@ -254,7 +256,7 @@ public async Task<ActionResult> Post([FromBody] DepartmentDTO depDTO)

//Save the changes and return the entity
await _departmentRepository.Update(department);
var members = _departmentdto.FindMembers(department.Members, _roleManager, _giraf);
var members = _girafUserService.FindMembers(department.Members, _roleManager, _giraf);
return CreatedAtRoute("GetDepartment", new { id = department.Key }, new SuccessResponse<DepartmentDTO>(new DepartmentDTO(department, members)));
}
catch (Exception e)
Expand Down
10 changes: 6 additions & 4 deletions GirafAPI/Controllers/PictogramController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using GirafEntities.WeekPlanner.DTOs;
using GirafRepositories.Interfaces;
using GirafServices.User;
using GirafServices.WeekPlanner;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -29,14 +30,15 @@ public class PictogramController : Controller
{
private const string IMAGE_TYPE_PNG = "image/png";

private readonly IGirafService _giraf;
private readonly IUserService _giraf;

private readonly IHostEnvironment _hostingEnvironment;

private readonly string imagePath;

private readonly IGirafUserRepository _girafUserRepository;
private readonly IPictogramRepository _pictogramRepository;
private readonly IImageService _imageService;

/// <summary>
/// Constructor for controller
Expand All @@ -47,7 +49,7 @@ public class PictogramController : Controller
/// <param name="girafUserRepository">The <see cref="IGirafUserRepository"/> used to query Users</param>
/// <param name="pictogramRepository">Pictogram Injection</param>
public PictogramController(
IGirafService girafService,
IUserService girafService,
IHostEnvironment hostingEnvironment,
ILoggerFactory lFactory,
IGirafUserRepository girafUserRepository,
Expand Down Expand Up @@ -343,7 +345,7 @@ public async Task<ActionResult> SetPictogramImage(long id)
}

//Update the image
byte[] image = await _giraf.ReadRequestImage(HttpContext.Request.Body);
byte[] image = await _imageService.ReadRequestImage(HttpContext.Request.Body);

// This sets the path that the system looks for when retrieving a pictogram
string path = imagePath + pictogram.Id + ".png";
Expand All @@ -363,7 +365,7 @@ public async Task<ActionResult> SetPictogramImage(long id)
return StatusCode(StatusCodes.Status403Forbidden, new ErrorResponse(ErrorCode.Forbidden, "The server does not have permission to write this file"));
}

pictogram.ImageHash = _giraf.GetHash(image);
pictogram.ImageHash = _imageService.GetHash(image);
}
_pictogramRepository.SaveState();
return Ok(new SuccessResponse<WeekPictogramDTO>(new WeekPictogramDTO(pictogram)));
Expand Down
4 changes: 2 additions & 2 deletions GirafAPI/Controllers/StatusController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace GirafAPI.Controllers
[Route("v1/[controller]")]
public class StatusController : Controller
{
private readonly IGirafService _giraf;
private readonly IUserService _giraf;

// SHOULD BE REMOVED AFTER REFACTORING OF THIS CONTROLLER HAS BEEN COMPLETED!
private readonly GirafDbContext _context;
Expand All @@ -26,7 +26,7 @@ public class StatusController : Controller
/// Constructor for StatusController
/// </summary>
/// <param name="giraf">Service Injection</param>
public StatusController(IGirafService giraf, GirafDbContext context)
public StatusController(IUserService giraf, GirafDbContext context)
{
_giraf = giraf;
_context = context;
Expand Down
4 changes: 2 additions & 2 deletions GirafAPI/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class UserController : Controller
{
private const int IMAGE_CONTENT_TYPE_DEFINITION = 25;
private const string IMAGE_TYPE_PNG = "image/png";
private readonly IGirafService _giraf;
private readonly IUserService _giraf;
private readonly IGirafUserRepository _girafUserRepository;
private readonly IImageRepository _imageRepository;
private readonly IUserResourseRepository _userResourseRepository;
Expand All @@ -51,7 +51,7 @@ public class UserController : Controller
/// <param name="pictogramRepository">Service Injection</param>
/// <param name="authentication"></param>
public UserController(
IGirafService giraf,
IUserService giraf,
ILoggerFactory loggerFactory,
RoleManager<GirafRole> roleManager,
IGirafUserRepository girafUserRepository,
Expand Down
10 changes: 6 additions & 4 deletions GirafAPI/Controllers/WeekController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using GirafEntities.WeekPlanner.DTOs;
using GirafRepositories.Interfaces;
using GirafServices.User;
using GirafServices.WeekPlanner;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -22,13 +23,14 @@ namespace GirafAPI.Controllers
[Route("v1/[controller]")]
public class WeekController : Controller
{
private readonly IGirafService _giraf;
private readonly IUserService _giraf;


private readonly IWeekRepository _weekRepository;
private readonly ITimerRepository _timerRepository;
private readonly IPictogramRepository _pictogramRepository;
private readonly IWeekdayRepository _weekdayRepository;
private readonly IWeekService _weekService;

/// <summary>
/// Constructor for WeekController
Expand All @@ -39,7 +41,7 @@ public class WeekController : Controller
/// <param name="timerRepository">Service Injection</param>
/// <param name="pictogramRepository">Service Injection</param>
/// <param name="weekdayRepository">Service Injection</param>
public WeekController(IGirafService giraf, ILoggerFactory loggerFactory, IWeekRepository weekRepository, ITimerRepository timerRepository, IPictogramRepository pictogramRepository, IWeekdayRepository weekdayRepository)
public WeekController(IUserService giraf, ILoggerFactory loggerFactory, IWeekRepository weekRepository, ITimerRepository timerRepository, IPictogramRepository pictogramRepository, IWeekdayRepository weekdayRepository)
{
_giraf = giraf;
_giraf._logger = loggerFactory.CreateLogger("Week");
Expand Down Expand Up @@ -290,7 +292,7 @@ public async Task<ActionResult> UpdateWeek(string userId, int weekYear, int week
user.WeekSchedule.Add(week);
}

var errorCode = await _weekRepository.SetWeekFromDTO(newWeek, week);
var errorCode = await _weekService.SetWeekFromDTO(newWeek, week);
if (errorCode != null)
return BadRequest(errorCode);

Expand Down Expand Up @@ -336,7 +338,7 @@ public async Task<ActionResult> UpdateWeekday(string userId, int weekYear, int w
Weekday oldDay = week.Weekdays.Single(d => d.Day == weekdayDto.Day);

oldDay.Activities.Clear();
if (!await _weekRepository.AddPictogramsToWeekday(oldDay, weekdayDto))
if (!await _weekService.AddPictogramsToWeekday(oldDay, weekdayDto))
{
return NotFound(new ErrorResponse(ErrorCode.ResourceNotFound, "Missing pictogram"));
}
Expand Down
8 changes: 4 additions & 4 deletions GirafAPI/Controllers/WeekTemplateController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class WeekTemplateController : Controller
/// <summary>
/// A reference to GirafService, that contains common functionality for all controllers.
/// </summary>
private readonly IGirafService _giraf;
private readonly IUserService _giraf;

/// <summary>
/// reference to the authenticationservice which provides commong authentication checks
Expand All @@ -45,7 +45,7 @@ public class WeekTemplateController : Controller
/// <param name="giraf">A reference to the GirafService.</param>
/// <param name="loggerFactory">A reference to an implementation of ILoggerFactory. Used to create a logger.</param>
/// <param name="authentication"></param>
public WeekTemplateController(IGirafService giraf,
public WeekTemplateController(IUserService giraf,
ILoggerFactory loggerFactory,
IAuthenticationService authentication,
GirafDbContext context,
Expand Down Expand Up @@ -155,7 +155,7 @@ public async Task<ActionResult> CreateWeekTemplate([FromBody] WeekTemplateDTO te

var newTemplate = new WeekTemplate(department);

var errorCode = await _weekService.SetWeekFromDTO(templateDto, newTemplate, _giraf);
var errorCode = await _weekService.SetWeekFromDTO(templateDto, newTemplate);
if (errorCode != null)
return BadRequest(errorCode);

Expand Down Expand Up @@ -208,7 +208,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 _weekService.SetWeekFromDTO(newValuesDto, template, _giraf);
var errorCode = await _weekService.SetWeekFromDTO(newValuesDto, template);
if (errorCode != null)
return BadRequest(errorCode);

Expand Down
Loading

0 comments on commit 6e7a8ea

Please sign in to comment.