Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 7f0fd41c

Přidáno uživatelem Milan Hotovec před téměř 4 roky(ů)

Lock update

Zobrazit rozdíly:

src/Modules/Goal/Application/Leuze.Modules.Goal.Application/CQRS/Comments/Commands/EditComment.cs
1
using Leuze.Core.Application.CQRS;
2
using Leuze.Core.Domain.Repositories;
3
using Leuze.Modules.Goal.Domain.Repositories;
4
using Microsoft.AspNetCore.Components.Authorization;
5
using System;
6
using System.Collections.Generic;
7
using System.Linq;
8
using System.Security.Claims;
9
using System.Text;
10
using System.Threading;
11
using System.Threading.Tasks;
12

  
13
namespace Leuze.Modules.Goal.Application.CQRS.Comments.Commands
14
{
15
    public static class EditComment
16
    {
17
        public record Command(Guid commentId, string text, bool isPrivate) : IBaseRequest<Response>;
18

  
19
        public record Response();
20

  
21

  
22
        public class Handler : IBaseRequestHandler<Command, Response>
23
        {
24
            private readonly ICommentRepository _commentRepo;
25
            private readonly AuthenticationStateProvider _authenticationStateProvider;
26
            private readonly IDomainUserRepository _domainUserRepo;
27

  
28
            public Handler(ICommentRepository commentRepo, AuthenticationStateProvider authStateProvider, IDomainUserRepository domainUserRepo)
29
            {
30
                _commentRepo = commentRepo;
31
                _authenticationStateProvider = authStateProvider;
32
                _domainUserRepo = domainUserRepo;
33
            }
34

  
35
            public async Task<RequestResponse<Response>> Handle(Command request, CancellationToken cancellationToken)
36
            {
37
                //TODO: user muze byt null, ale to my nechceme!!!
38
                var state = await _authenticationStateProvider.GetAuthenticationStateAsync();
39
                var id = Guid.Parse(state.User!.FindFirst(ClaimTypes.NameIdentifier)!.Value);
40
                var editor = await _domainUserRepo.GetByIdAsync(id);
41
                var comment = await _commentRepo.GetByIdAsync(request.commentId);
42

  
43
                if (editor == null)
44
                {
45
                    return RequestResponse<Response>.CreateErrorResponse($"Editor user with id: {id} not found!");
46
                }
47
                if (comment == null)
48
                {
49
                    return RequestResponse<Response>.CreateErrorResponse($"Comment with id: {request.commentId} not found!");
50
                }
51
                if (editor.UserId != comment.CreatorId)
52
                {
53
                    return RequestResponse<Response>.CreateErrorResponse($"Editor and comment creator are not the same person!");
54
                }
55
                if (string.IsNullOrWhiteSpace(request.text))
56
                {
57
                    return RequestResponse<Response>.CreateErrorResponse($"Comment text is empty!");
58
                }
59

  
60
                comment.SetText(request.text);
61

  
62
                if (request.isPrivate)
63
                {
64
                    comment.SetPrivate();
65
                }
66
                else
67
                {
68
                    comment.SetPublic();
69
                }
70

  
71
                await _commentRepo.UpdateRepo();
72

  
73
                return RequestResponse<Response>.CreateSuccessResponse(new());
74
            }
75
        }
76
    }
77
}
src/Modules/Goal/Application/Leuze.Modules.Goal.Application/CQRS/Comments/Commands/RemoveComment.cs
1
using Leuze.Core.Application.CQRS;
2
using Leuze.Core.Domain.Repositories;
3
using Leuze.Modules.Goal.Domain.Repositories;
4
using Microsoft.AspNetCore.Components.Authorization;
5
using System;
6
using System.Collections.Generic;
7
using System.Linq;
8
using System.Security.Claims;
9
using System.Text;
10
using System.Threading;
11
using System.Threading.Tasks;
12

  
13
namespace Leuze.Modules.Goal.Application.CQRS.Comments.Commands
14
{
15
    public static class RemoveComment
16
    {
17
        public record Command(Guid commentId) : IBaseRequest<Response>;
18

  
19
        public record Response();
20

  
21

  
22
        public class Handler : IBaseRequestHandler<Command, Response>
23
        {
24
            private readonly ICommentRepository _commentRepo;
25
            private readonly AuthenticationStateProvider _authenticationStateProvider;
26
            private readonly IDomainUserRepository _domainUserRepo;
27

  
28
            public Handler(ICommentRepository commentRepo, AuthenticationStateProvider authStateProvider, IDomainUserRepository domainUserRepo)
29
            {
30
                _commentRepo = commentRepo;
31
                _authenticationStateProvider = authStateProvider;
32
                _domainUserRepo = domainUserRepo;
33
            }
34

  
35
            public async Task<RequestResponse<Response>> Handle(Command request, CancellationToken cancellationToken)
36
            {
37
                //TODO: user muze byt null, ale to my nechceme!!!
38
                var state = await _authenticationStateProvider.GetAuthenticationStateAsync();
39
                var id = Guid.Parse(state.User!.FindFirst(ClaimTypes.NameIdentifier)!.Value);
40
                var editor = await _domainUserRepo.GetByIdAsync(id);
41
                var comment = await _commentRepo.GetByIdAsync(request.commentId);
42

  
43
                if (editor == null)
44
                {
45
                    return RequestResponse<Response>.CreateErrorResponse($"Editor user with id: {id} not found!");
46
                }
47
                if (comment == null)
48
                {
49
                    return RequestResponse<Response>.CreateErrorResponse($"Comment with id: {request.commentId} not found!");
50
                }
51
                if (editor.UserId != comment.CreatorId)
52
                {
53
                    return RequestResponse<Response>.CreateErrorResponse($"Editor and comment creator are not the same person!");
54
                }
55

  
56
                await _commentRepo.RemoveAsync(comment.Id);
57

  
58
                return RequestResponse<Response>.CreateSuccessResponse(new());
59
            }
60
        }
61
    }
62
}
src/Modules/Goal/Application/Leuze.Modules.Goal.Application/CQRS/Goals/Commands/CreateGoal.cs
51 51
                    return RequestResponse<Response>.CreateErrorResponse($"Goal definition with id: {request.GoalDefinitionId} not found!");
52 52
                }
53 53

  
54
                if (goalDef.IsLocked())
55
                {
56
                    return RequestResponse<Response>.CreateErrorResponse($"Goal definition with id: {request.GoalDefinitionId} is write protected!");
57
                }
58

  
54 59
                GoalItem item = new(goalDef, user, request.Name, request.Description);
55 60
                await _goalItemRepo.AddAsync(item);
56 61

  
src/Modules/Goal/Application/Leuze.Modules.Goal.Application/CQRS/Goals/Commands/EditGoal.cs
50 50
                    return RequestResponse<Response>.CreateErrorResponse($"Cíl s ID: {request.goalId} nebyl nalezen!");
51 51
                }
52 52

  
53
                if(request.name == null || request.name == "")
53
                if (goal.IsLocked())
54
                {
55
                    return RequestResponse<Response>.CreateErrorResponse($"Cíl s ID: {request.goalId} byl uzamčen proti přepsání!");
56
                }
57

  
58
                if (goal.GoalDefinition.IsLocked())
59
                {
60
                    return RequestResponse<Response>.CreateErrorResponse($"Definice s cílem je uzamčena proti přepsání!");
61
                }
62

  
63
                if (request.name == null || request.name == "")
54 64
                {
55 65
                    return RequestResponse<Response>.CreateErrorResponse($"Nelze změnit název cíle");
56 66
                }
src/Modules/Goal/Application/Leuze.Modules.Goal.Application/CQRS/Goals/Commands/RemoveGoal.cs
22 22

  
23 23
            public async Task<RequestResponse<Response>> Handle(Command request, CancellationToken cancellationToken)
24 24
            {
25
                var item = await _repository.GetByIdAsync(request.Id);
26

  
27
                if(item == null)
28
                {
29
                    return RequestResponse<Response>.CreateErrorResponse($"Goal with ID '{request.Id}' not found");
30
                }
31
                if (item.IsLocked())
32
                {
33
                    return RequestResponse<Response>.CreateErrorResponse($"Goal is write protected!");
34
                }
35
                if (item.GoalDefinition.IsLocked())
36
                {
37
                    return RequestResponse<Response>.CreateErrorResponse($"Goal definition of item is write protected!");
38
                }
39

  
25 40
                if (!await _repository.RemoveAsync(request.Id))
26 41
                    return RequestResponse<Response>.CreateErrorResponse($"Goal with ID '{request.Id}' not found");
27 42

  
src/Modules/Goal/Domain/Leuze.Modules.Goal.Domain/Domains/GoalDefinition.cs
83 83
            this.OwnerLocked = false;
84 84
            this.SupervisorLocked = false;
85 85
        }
86

  
87
        public bool IsLocked()
88
        {
89
            return SupervisorLocked && OwnerLocked;
90
        }
86 91
    }
87 92
}
src/Modules/Goal/Domain/Leuze.Modules.Goal.Domain/Domains/GoalItem.cs
85 85
            this.OwnerLocked = false;
86 86
            this.SupervisorLocked = false;
87 87
        }
88

  
89
        public bool IsLocked()
90
        {
91
            return SupervisorLocked && OwnerLocked;
92
        }
88 93
    }
89 94
}
src/Modules/Goal/Infrastructure/Leuze.Modules.Goal.Infrastructure.Persistence/Repositories/GoalItemRepository.cs
38 38

  
39 39
        public async Task<GoalItem> GetByIdAsync(Guid id)
40 40
        {
41
            return await dbSet.FindAsync(id);
41
            return await dbSet.Where(o => o.Id == id).Include(o => o.GoalDefinition).FirstOrDefaultAsync();
42 42
        }
43 43

  
44 44
        public async Task<List<GoalItem>> GetAllForUserDefinitionAsync(Guid definitionId)

Také k dispozici: Unified diff