|
1 | | -using Jordnaer.Database; |
2 | | -using Jordnaer.Shared; |
3 | | -using Microsoft.EntityFrameworkCore; |
4 | | -using OneOf; |
5 | | -using OneOf.Types; |
6 | | - |
7 | | -namespace Jordnaer.Features.GroupPosts; |
8 | | - |
9 | | - |
10 | | - |
11 | | -public class GroupPostService(IDbContextFactory<JordnaerDbContext> contextFactory) |
12 | | -{ |
13 | | - public async Task<OneOf<GroupPostDto, NotFound>> GetPostAsync(Guid postId, |
14 | | - CancellationToken cancellationToken = default) |
15 | | - { |
16 | | - await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); |
17 | | - |
18 | | - var post = await context.GroupPosts |
19 | | - .AsNoTracking() |
20 | | - .Where(x => x.Id == postId) |
21 | | - .Select(x => x.ToGroupPostDto()) |
22 | | - .FirstOrDefaultAsync(cancellationToken); |
23 | | - |
24 | | - return post is null |
25 | | - ? new NotFound() |
26 | | - : post; |
27 | | - } |
28 | | - |
29 | | - public async Task<List<GroupPostDto>> GetPostsAsync(Guid groupId, |
30 | | - CancellationToken cancellationToken = default) |
31 | | - { |
32 | | - await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); |
33 | | - |
34 | | - var posts = await context.GroupPosts |
35 | | - .AsNoTracking() |
36 | | - .Where(x => x.GroupId == groupId) |
37 | | - .OrderByDescending(x => x.CreatedUtc) |
38 | | - .Include(x => x.UserProfile) |
39 | | - .ToListAsync(cancellationToken); |
40 | | - |
41 | | - return posts.Select(x => x.ToGroupPostDto()).ToList(); |
42 | | - } |
43 | | - |
44 | | - public async Task<OneOf<Success, Error<string>>> CreatePostAsync(GroupPost post, |
45 | | - CancellationToken cancellationToken = default) |
46 | | - { |
47 | | - await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); |
48 | | - |
49 | | - if (await context.GroupPosts |
50 | | - .AsNoTracking() |
51 | | - .AnyAsync(x => x.Id == post.Id, |
52 | | - cancellationToken)) |
53 | | - { |
54 | | - return new Error<string>("Opslaget eksisterer allerede."); |
55 | | - } |
56 | | - |
57 | | - context.GroupPosts.Add(post); |
58 | | - |
59 | | - await context.SaveChangesAsync(cancellationToken); |
60 | | - |
61 | | - return new Success(); |
62 | | - } |
63 | | - |
64 | | - public async Task<OneOf<Success, Error<string>>> DeletePostAsync(Guid postId, string userId, |
65 | | - CancellationToken cancellationToken = default) |
66 | | - { |
67 | | - await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); |
68 | | - |
69 | | - var rowsDeleted = await context.GroupPosts |
70 | | - .Where(x => x.Id == postId && x.UserProfileId == userId) |
71 | | - .ExecuteDeleteAsync(cancellationToken); |
72 | | - |
73 | | - return rowsDeleted > 0 |
74 | | - ? new Success() |
75 | | - : new Error<string>("Opslaget blev ikke fundet eller du har ikke rettigheder til at slette det."); |
76 | | - } |
77 | | -} |
| 1 | +using Jordnaer.Database; |
| 2 | +using Jordnaer.Shared; |
| 3 | +using Microsoft.EntityFrameworkCore; |
| 4 | +using OneOf; |
| 5 | +using OneOf.Types; |
| 6 | + |
| 7 | +namespace Jordnaer.Features.GroupPosts; |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +public class GroupPostService(IDbContextFactory<JordnaerDbContext> contextFactory) |
| 12 | +{ |
| 13 | + public async Task<OneOf<GroupPostDto, NotFound>> GetPostAsync(Guid postId, |
| 14 | + CancellationToken cancellationToken = default) |
| 15 | + { |
| 16 | + await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); |
| 17 | + |
| 18 | + var post = await context.GroupPosts |
| 19 | + .AsNoTracking() |
| 20 | + .Where(x => x.Id == postId) |
| 21 | + .Select(x => x.ToGroupPostDto()) |
| 22 | + .FirstOrDefaultAsync(cancellationToken); |
| 23 | + |
| 24 | + return post is null |
| 25 | + ? new NotFound() |
| 26 | + : post; |
| 27 | + } |
| 28 | + |
| 29 | + public async Task<List<GroupPostDto>> GetPostsAsync(Guid groupId, |
| 30 | + CancellationToken cancellationToken = default) |
| 31 | + { |
| 32 | + await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); |
| 33 | + |
| 34 | + var posts = await context.GroupPosts |
| 35 | + .AsNoTracking() |
| 36 | + .Where(x => x.GroupId == groupId) |
| 37 | + .OrderByDescending(x => x.CreatedUtc) |
| 38 | + .Include(x => x.UserProfile) |
| 39 | + .ToListAsync(cancellationToken); |
| 40 | + |
| 41 | + return posts.Select(x => x.ToGroupPostDto()).ToList(); |
| 42 | + } |
| 43 | + |
| 44 | + public async Task<OneOf<Success, Error<string>>> CreatePostAsync(GroupPost post, |
| 45 | + CancellationToken cancellationToken = default) |
| 46 | + { |
| 47 | + await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); |
| 48 | + |
| 49 | + if (await context.GroupPosts |
| 50 | + .AsNoTracking() |
| 51 | + .AnyAsync(x => x.Id == post.Id, |
| 52 | + cancellationToken)) |
| 53 | + { |
| 54 | + return new Error<string>("Opslaget eksisterer allerede."); |
| 55 | + } |
| 56 | + |
| 57 | + context.GroupPosts.Add(post); |
| 58 | + |
| 59 | + await context.SaveChangesAsync(cancellationToken); |
| 60 | + |
| 61 | + return new Success(); |
| 62 | + } |
| 63 | + |
| 64 | + public async Task<OneOf<Success, Error<string>>> DeletePostAsync(Guid postId, string userId, |
| 65 | + CancellationToken cancellationToken = default) |
| 66 | + { |
| 67 | + await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); |
| 68 | + |
| 69 | + var rowsDeleted = await context.GroupPosts |
| 70 | + .Where(x => x.Id == postId && x.UserProfileId == userId) |
| 71 | + .ExecuteDeleteAsync(cancellationToken); |
| 72 | + |
| 73 | + return rowsDeleted > 0 |
| 74 | + ? new Success() |
| 75 | + : new Error<string>("Opslaget blev ikke fundet eller du har ikke rettigheder til at slette det."); |
| 76 | + } |
| 77 | +} |
0 commit comments