Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a repository for departments #525

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 16 additions & 21 deletions backend/Api/Organisation/DeparmentController.cs
Original file line number Diff line number Diff line change
@@ -1,52 +1,47 @@
using System.ComponentModel.DataAnnotations;
using Core.Organizations;
using Infrastructure.DatabaseContext;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace Api.Organisation;

[Route("/v0/organisations")]
[ApiController]
public class OrganisationController : ControllerBase
public class OrganisationController(ApplicationContext applicationContext, IDepartmentRepository departmentRepository)
: ControllerBase
{
private readonly ApplicationContext _applicationContext;

public OrganisationController(ApplicationContext applicationContext)
{
_applicationContext = applicationContext;
}

[HttpGet]
public ActionResult<List<OrganisationReadModel>> Get()
{
return _applicationContext.Organization
return applicationContext.Organization
.Select(organization => new OrganisationReadModel(organization.Name, organization.UrlKey))
.ToList();
}


[HttpGet]
[Route("{orgUrlKey}/departments")]
public ActionResult<List<DepartmentReadModel>> GetDepartment([FromRoute] string orgUrlKey)
public async Task<ActionResult<List<DepartmentReadModel>>> GetDepartment([FromRoute] string orgUrlKey,
CancellationToken cancellationToken)
{
return _applicationContext.Organization
.Include(o => o.Departments)
.Single(o => o.UrlKey == orgUrlKey)
.Departments
.Select(d => new DepartmentReadModel(d.Id, d.Name, d.Hotkey))
.ToList();
var departments = await departmentRepository.GetDepartmentsInOrganizationByUrlKey(orgUrlKey, cancellationToken);
return departments.Select(department => new DepartmentReadModel(department)).ToList();
}

[HttpGet]
[Route("{orgUrlKey}/weeklyWorkHours")]
public ActionResult<double> GetWeeklyWorkHours([FromRoute] string orgUrlKey)
{
return _applicationContext.Organization
return applicationContext.Organization
.Single(o => o.UrlKey == orgUrlKey)
.HoursPerWorkday * 5;
}
}

public record DepartmentReadModel([property: Required] string Id, [property: Required] string Name, int? Hotkey);
public record DepartmentReadModel(string Id, string Name, int? Hotkey)
{
public DepartmentReadModel(Department d) : this(d.Id, d.Name, d.Hotkey)
{
}
}

public record OrganisationReadModel([property: Required] string Name, [property: Required] string UrlKey);
public record OrganisationReadModel(string Name, string UrlKey);
1 change: 1 addition & 0 deletions backend/Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Api.AppExtensions;
using Api.Options;
using Infrastructure.DatabaseContext;
using Infrastructure.Repositories;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.Identity.Web;
Expand Down
7 changes: 7 additions & 0 deletions backend/Core/Organizations/IDepartmentRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Core.Organizations;

public interface IDepartmentRepository
{
public Task<List<Department>> GetDepartmentsInOrganizationByUrlKey(string orgUrlKey,
CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
using Infrastructure.DatabaseContext;
using Microsoft.EntityFrameworkCore;

namespace Infrastructure.Repositories;
namespace Infrastructure.Repositories.Engagement;

public class EngagementDbRepository(ApplicationContext context) : IEngagementRepository
{
public Task<Engagement?> GetEngagementById(int id, CancellationToken cancellationToken)
public Task<Core.Engagements.Engagement?> GetEngagementById(int id, CancellationToken cancellationToken)
{
return context.Project
.AsNoTracking()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Core.Organizations;
using Infrastructure.DatabaseContext;
using Microsoft.EntityFrameworkCore;

namespace Infrastructure.Repositories.Organization;

public class DepartmentDbRepository(ApplicationContext context) : IDepartmentRepository
{
public async Task<List<Department>> GetDepartmentsInOrganizationByUrlKey(string orgUrlKey,
CancellationToken cancellationToken)
{
var organizationId = context.Organization.FirstOrDefault(o => o.UrlKey == orgUrlKey)?.Id;
if (organizationId is null) return [];

return await context.Department.Where(d => d.Organization.Id == organizationId).ToListAsync(cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
using Infrastructure.DatabaseContext;
using Microsoft.EntityFrameworkCore;

namespace Infrastructure.Repositories;
namespace Infrastructure.Repositories.Organization;

public class OrganisationDbRepository(ApplicationContext context) : IOrganisationRepository
{
public Task<Organization?> GetOrganizationByUrlKey(string urlKey, CancellationToken cancellationToken)
public Task<Core.Organizations.Organization?> GetOrganizationByUrlKey(string urlKey,
CancellationToken cancellationToken)
{
return context.Organization
.AsNoTracking()
Expand Down
6 changes: 4 additions & 2 deletions backend/Infrastructure/Repositories/RepositoryExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
using Core.Engagements;
using Core.Organizations;
using Infrastructure.Repositories;
using Infrastructure.Repositories.Engagement;
using Infrastructure.Repositories.Organization;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace Api.AppExtensions;
namespace Infrastructure.Repositories;

public static class RepositoryExtensions
{
public static void AddRepositories(this WebApplicationBuilder builder)
{
builder.Services.AddScoped<IOrganisationRepository, OrganisationDbRepository>();
builder.Services.AddScoped<IEngagementRepository, EngagementDbRepository>();
builder.Services.AddScoped<IDepartmentRepository, DepartmentDbRepository>();
}
}
Loading