aboutsummaryrefslogtreecommitdiffstats
path: root/code/api/src/Endpoints/V1/Projects
diff options
context:
space:
mode:
authorivarlovlie <git@ivarlovlie.no>2022-10-30 16:40:03 +0100
committerivarlovlie <git@ivarlovlie.no>2022-10-30 16:40:03 +0100
commit0725e4f7cf4c6f723264b6d461b91c660d144cb7 (patch)
treeaae5876b5760c80679161d918c34d753ec0e2582 /code/api/src/Endpoints/V1/Projects
parentd76c180c9631df015d37138045c79a46cca350e8 (diff)
downloadgreatoffice-0725e4f7cf4c6f723264b6d461b91c660d144cb7.tar.xz
greatoffice-0725e4f7cf4c6f723264b6d461b91c660d144cb7.zip
feat: Apiwork
Diffstat (limited to 'code/api/src/Endpoints/V1/Projects')
-rw-r--r--code/api/src/Endpoints/V1/Projects/CreateProjectRoute.cs80
-rw-r--r--code/api/src/Endpoints/V1/Projects/GetProjectsRoute.cs43
-rw-r--r--code/api/src/Endpoints/V1/Projects/_calls.http12
3 files changed, 135 insertions, 0 deletions
diff --git a/code/api/src/Endpoints/V1/Projects/CreateProjectRoute.cs b/code/api/src/Endpoints/V1/Projects/CreateProjectRoute.cs
new file mode 100644
index 0000000..5c78e27
--- /dev/null
+++ b/code/api/src/Endpoints/V1/Projects/CreateProjectRoute.cs
@@ -0,0 +1,80 @@
+using Microsoft.Extensions.Localization;
+
+namespace IOL.GreatOffice.Api.Endpoints.V1.Projects;
+
+public class CreateProjectRoute : RouteBaseAsync.WithRequest<CreateProjectPayload>.WithActionResult
+{
+ private readonly MainAppDatabase _database;
+ private readonly IStringLocalizer<SharedResources> _localizer;
+
+ public CreateProjectRoute(MainAppDatabase database, IStringLocalizer<SharedResources> localizer) {
+ _database = database;
+ _localizer = localizer;
+ }
+
+ [HttpPost("~/v{version:apiVersion}/projects/create")]
+ public override async Task<ActionResult> HandleAsync(CreateProjectPayload request, CancellationToken cancellationToken = default) {
+ var errors = new Dictionary<string, string>();
+
+ if (request.Name.IsNullOrEmpty()) {
+ errors.Add("name", _localizer["Name is a required field"]);
+ }
+
+ var project = new Project(LoggedInUser) {
+ Name = request.Name,
+ Description = request.Description,
+ Start = request.Start,
+ Stop = request.Stop,
+ };
+ project.SetOwnerIds(default, LoggedInUser.TenantId);
+
+ foreach (var customerId in request.CustomerIds) {
+ var customer = _database.Customers.FirstOrDefault(c => c.Id == customerId);
+ if (customer == default) {
+ errors.Add("customer_" + customerId, _localizer["Customer not found"]);
+ continue;
+ }
+
+ project.Customers.Add(customer);
+ }
+
+ foreach (var member in request.Members) {
+ var user = _database.Users.FirstOrDefault(c => c.Id == member.UserId);
+ if (user == default) {
+ errors.Add("members_" + member.UserId, _localizer["User not found"]);
+ continue;
+ }
+
+ project.Members.Add(new ProjectMember() {
+ Project = project,
+ User = user,
+ Role = member.Role
+ });
+ }
+
+ if (errors.Any()) return KnownProblem(_localizer["Invalid form"], _localizer["One or more fields is invalid"], errors);
+
+ _database.Projects.Add(project);
+ await _database.SaveChangesAsync(cancellationToken);
+ return Ok();
+ }
+}
+
+public class CreateProjectResponse
+{ }
+
+public class CreateProjectPayload
+{
+ public string Name { get; set; }
+ public string Description { get; set; }
+ public DateTime Start { get; set; }
+ public DateTime Stop { get; set; }
+ public List<Guid> CustomerIds { get; set; }
+ public List<ProjectMember> Members { get; set; }
+
+ public class ProjectMember
+ {
+ public Guid UserId { get; set; }
+ public ProjectRole Role { get; set; }
+ }
+} \ No newline at end of file
diff --git a/code/api/src/Endpoints/V1/Projects/GetProjectsRoute.cs b/code/api/src/Endpoints/V1/Projects/GetProjectsRoute.cs
new file mode 100644
index 0000000..d60f974
--- /dev/null
+++ b/code/api/src/Endpoints/V1/Projects/GetProjectsRoute.cs
@@ -0,0 +1,43 @@
+using MR.AspNetCore.Pagination;
+
+namespace IOL.GreatOffice.Api.Endpoints.V1.Projects;
+
+public class GetProjectsRoute : RouteBaseAsync.WithRequest<GetProjectsQueryParameters>.WithActionResult<KeysetPaginationResult<GetProjectsResponseDto>>
+{
+ private readonly MainAppDatabase _database;
+ private readonly PaginationService _pagination;
+
+ public GetProjectsRoute(MainAppDatabase database, PaginationService pagination) {
+ _database = database;
+ _pagination = pagination;
+ }
+
+ [HttpGet("~/v{version:apiVersion}/projects")]
+ public override async Task<ActionResult<KeysetPaginationResult<GetProjectsResponseDto>>> HandleAsync(GetProjectsQueryParameters request, CancellationToken cancellationToken = default) {
+ var result = await _pagination.KeysetPaginateAsync(
+ _database.Projects.ForTenant(LoggedInUser),
+ b => b.Descending(x => x.CreatedAt),
+ async id => await _database.Projects.FindAsync(id),
+ query => query.Select(p => new GetProjectsResponseDto() {
+ Id = p.Id,
+ Name = p.Name
+ })
+ );
+ return Ok(result);
+ }
+}
+
+public class GetProjectsResponseDto
+{
+ public Guid Id { get; set; }
+ public string Name { get; set; }
+}
+
+public class GetProjectsQueryParameters
+{
+ [FromQuery]
+ public int Page { get; set; }
+
+ [FromQuery]
+ public int Size { get; set; }
+} \ No newline at end of file
diff --git a/code/api/src/Endpoints/V1/Projects/_calls.http b/code/api/src/Endpoints/V1/Projects/_calls.http
new file mode 100644
index 0000000..af0eba6
--- /dev/null
+++ b/code/api/src/Endpoints/V1/Projects/_calls.http
@@ -0,0 +1,12 @@
+### Create Project
+GET https://localhost:5001/v1/projects/create
+Accept: application/json
+Content-Type: application/json
+Cookie: ""
+
+{
+ "": ""
+}
+
+### Get Projects
+POST http://localhost