| | | 1 | | // SPDX-FileCopyrightText: 2026 Alper Çelik <[email protected]> |
| | | 2 | | // |
| | | 3 | | // SPDX-License-Identifier: AGPL-3.0-or-later |
| | | 4 | | |
| | | 5 | | using System.ComponentModel.DataAnnotations; |
| | | 6 | | |
| | | 7 | | using Api.Auth.Handlers; |
| | | 8 | | using Api.Auth.Models; |
| | | 9 | | using Api.Auth.Utils; |
| | | 10 | | using Api.Database; |
| | | 11 | | using Api.Utils; |
| | | 12 | | using Api.Works.DTOs; |
| | | 13 | | |
| | | 14 | | using Microsoft.AspNetCore.Http.HttpResults; |
| | | 15 | | using Microsoft.AspNetCore.Mvc; |
| | | 16 | | using Microsoft.EntityFrameworkCore; |
| | | 17 | | |
| | | 18 | | namespace Api.Works.Endpoints; |
| | | 19 | | |
| | | 20 | | public static class TagEndpoints |
| | | 21 | | { |
| | | 22 | | |
| | | 23 | | public static void Map(IEndpointRouteBuilder route) |
| | 0 | 24 | | { |
| | 0 | 25 | | var tags = route.MapGroup("tags"); |
| | | 26 | | |
| | 0 | 27 | | tags.MapMethods("", [HttpMethod.Query.Method, HttpMethod.Get.Method], QueryTags); |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | [PermissionCheckAuthorize(UserPermissionBits.WorkTagRead | UserPermissionBits.WorkRead)] |
| | | 31 | | public static async Task<Results< |
| | | 32 | | Ok<PaginationResult<TagGetDTO>>, |
| | | 33 | | NotFound, |
| | | 34 | | BadRequest>> |
| | | 35 | | QueryTags( |
| | | 36 | | [FromServices] PGContext db, |
| | | 37 | | [FromServices] ICurrentUserId userId, |
| | | 38 | | |
| | | 39 | | [FromBody] TagQueryDTO queryDTO |
| | | 40 | | ) |
| | 0 | 41 | | { |
| | 0 | 42 | | if (userId.Id is null) |
| | 0 | 43 | | return TypedResults.BadRequest(); |
| | | 44 | | |
| | 0 | 45 | | var results = db.WorkTags |
| | 0 | 46 | | .Where(wt => wt.OwnerId == userId.Id); |
| | | 47 | | |
| | 0 | 48 | | if (queryDTO.TagIds.Any()) |
| | 0 | 49 | | results = results.Where(wt => queryDTO.TagIds.Contains(wt.Id)); |
| | | 50 | | |
| | 0 | 51 | | if (queryDTO.TagTypes.Any()) |
| | 0 | 52 | | results = results.Where(wt => queryDTO.TagTypes.Contains(wt.TagType)); |
| | | 53 | | |
| | 0 | 54 | | if (queryDTO.WorkIds.Any()) |
| | 0 | 55 | | results = results |
| | 0 | 56 | | .Include(wt => wt.WorkTagWorks) |
| | 0 | 57 | | .Where(wt => |
| | 0 | 58 | | wt.WorkTagWorks.Any( |
| | 0 | 59 | | wtw => queryDTO.WorkIds.Contains(wtw.WorkId))); |
| | | 60 | | |
| | 0 | 61 | | foreach (var sort in queryDTO.SortOptions.Distinct()) |
| | 0 | 62 | | { |
| | 0 | 63 | | results = sort switch |
| | 0 | 64 | | { |
| | 0 | 65 | | TagSortOption.Id => results.OrderBy(wt => wt.Id), |
| | 0 | 66 | | TagSortOption.TagType => results.OrderBy(wt => wt.TagType), |
| | 0 | 67 | | TagSortOption.TagName => results.OrderBy(wt => wt.TagName), |
| | 0 | 68 | | _ => results |
| | 0 | 69 | | }; |
| | 0 | 70 | | } |
| | 0 | 71 | | var totalCount = await results.CountAsync(); |
| | | 72 | | |
| | 0 | 73 | | if (queryDTO.Page * queryDTO.PageSize >= totalCount) |
| | 0 | 74 | | return TypedResults.NotFound(); |
| | | 75 | | |
| | 0 | 76 | | return TypedResults.Ok(new PaginationResult<TagGetDTO>( |
| | 0 | 77 | | await results.Skip(queryDTO.PageSize * (queryDTO.Page - 1)).Take(queryDTO.PageSize).ProjectToDTO().T |
| | 0 | 78 | | totalCount, |
| | 0 | 79 | | queryDTO.PageSize, |
| | 0 | 80 | | queryDTO.Page, |
| | 0 | 81 | | (int)Math.Ceiling(totalCount / (double)queryDTO.PageSize) |
| | 0 | 82 | | )); |
| | | 83 | | |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | } |