Skip to content

Commit

Permalink
Feat/update project name backend (#501)
Browse files Browse the repository at this point in the history
  • Loading branch information
idamand committed Jul 24, 2024
1 parent 89920b0 commit e25ffc9
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 0 deletions.
35 changes: 35 additions & 0 deletions backend/Api/Projects/ProjectController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,41 @@ public ActionResult<List<StaffingReadModel>> Put([FromRoute] string orgUrlKey,
}
}

[HttpPut]
[Route("updateProjectName")]
public ActionResult<List<EngagementReadModel>> Put([FromRoute] string orgUrlKey,
[FromBody] UpdateProjectNameWriteModel projectWriteModel)
{
// Merk: Service kommer snart via Dependency Injection, da slipper å lage ny hele tiden
var service = new StorageService(_cache, _context);

if (!ProjectControllerValidator.ValidateUpdateProjectNameWriteModel(projectWriteModel, service, orgUrlKey))
return BadRequest("Error in data");
if (ProjectControllerValidator.ValidateUpdateProjectNameAlreadyExist(projectWriteModel, service, orgUrlKey))
{return BadRequest("Name already in use");}

try
{
Engagement engagement;
engagement = _context.Project
.Include(p => p.Consultants)
.Include(p => p.Staffings)
.Single(p => p.Id == projectWriteModel.EngagementId);

engagement.Name = projectWriteModel.EngagementName;
_context.SaveChanges();

service.ClearConsultantCache(orgUrlKey);

return Ok();
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}

private bool EngagementHasSoftMatch(int id)
{
var engagementToChange = _context.Project
Expand Down
22 changes: 22 additions & 0 deletions backend/Api/Projects/ProjectControllerValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@ public static bool ValidateUpdateProjectWriteModel(UpdateProjectWriteModel updat
CheckIfEngagementIsInOrganisation(updateProjectWriteModel.EngagementId, storageService, orgUrlKey);
}

public static bool ValidateUpdateProjectNameWriteModel(UpdateProjectNameWriteModel updateProjectNameWriteModel, StorageService storageService, string orgUrlKey)
{
return CheckIfEngagementExists(updateProjectNameWriteModel.EngagementId, storageService) &&
CheckIfEngagementIsInOrganisation(updateProjectNameWriteModel.EngagementId, storageService, orgUrlKey) && !string.IsNullOrWhiteSpace(updateProjectNameWriteModel.EngagementName);
}

public static bool ValidateUpdateProjectNameAlreadyExist(UpdateProjectNameWriteModel updateProjectNameWriteModel,
StorageService storageService, string orgUrlKey)
{
var updatedEngagement = storageService.GetProjectById(updateProjectNameWriteModel.EngagementId);
if (updatedEngagement is not null)
{
var customer = storageService.GetCustomerFromId(orgUrlKey, updatedEngagement.CustomerId);
if (customer is not null)
{
return customer.Projects.Any(engagement => string.Equals(engagement.Name,
updateProjectNameWriteModel.EngagementName, StringComparison.OrdinalIgnoreCase));
}
}
return false;
}

private static bool CheckIfEngagementExists(int engagementId, StorageService storageService)
{
return storageService.GetProjectById(engagementId) is not null;
Expand Down
2 changes: 2 additions & 0 deletions backend/Api/Projects/ProjectModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public record ProjectWithCustomerModel(
public record UpdateProjectWriteModel(int EngagementId, EngagementState ProjectState, int StartYear, int StartWeek,
int WeekSpan);

public record UpdateProjectNameWriteModel(int EngagementId, string EngagementName);


public record CustomersWithProjectsReadModel(
[property: Required] int CustomerId,
Expand Down
7 changes: 7 additions & 0 deletions frontend/src/api-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,13 @@ export interface UpdateProjectWriteModel {
weekSpan?: number;
}

export interface UpdateProjectNameWriteModel {
/** @format int32 */
engagementId?: number;
/** @minLength 1 */
projectName: string;
}

export interface VacationMetaData {
/** @format int32 */
daysTotal?: number;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { putWithToken } from "@/data/apiCallsWithToken";
import { updateProjectNameBody } from "@/types";
import { NextResponse } from "next/server";
import { EngagementReadModel } from "@/api-types";

export async function PUT(
request: Request,
{ params }: { params: { organisation: string } },
) {
const orgUrlKey = params.organisation;
const requestBody = (await request.json()) as updateProjectNameBody;

const project =
(await putWithToken<EngagementReadModel, updateProjectNameBody>(
`${orgUrlKey}/projects/updateProjectName`,
requestBody,
)) ?? [];

return NextResponse.json(project);
}
5 changes: 5 additions & 0 deletions frontend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ export interface updateProjectStateBody {
weekSpan: number;
}

export interface updateProjectNameBody {
engagementId: string;
engagementName: string;
}

export interface WeekWithHours {
week: number;
hours: number;
Expand Down

0 comments on commit e25ffc9

Please sign in to comment.