commit 7f0fd41cca63fa84097947154ac992dc555b877e
Author: Silentkiller159 <milanhotovec@seznam.cz>
Date:   Tue Jun 1 23:05:27 2021 +0200

    Lock update

diff --git a/src/Modules/Goal/Application/Leuze.Modules.Goal.Application/CQRS/Comments/Commands/EditComment.cs b/src/Modules/Goal/Application/Leuze.Modules.Goal.Application/CQRS/Comments/Commands/EditComment.cs
new file mode 100644
index 0000000..b9202ee
--- /dev/null
+++ b/src/Modules/Goal/Application/Leuze.Modules.Goal.Application/CQRS/Comments/Commands/EditComment.cs
@@ -0,0 +1,77 @@
+﻿using Leuze.Core.Application.CQRS;
+using Leuze.Core.Domain.Repositories;
+using Leuze.Modules.Goal.Domain.Repositories;
+using Microsoft.AspNetCore.Components.Authorization;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Security.Claims;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace Leuze.Modules.Goal.Application.CQRS.Comments.Commands
+{
+    public static class EditComment
+    {
+        public record Command(Guid commentId, string text, bool isPrivate) : IBaseRequest<Response>;
+
+        public record Response();
+
+
+        public class Handler : IBaseRequestHandler<Command, Response>
+        {
+            private readonly ICommentRepository _commentRepo;
+            private readonly AuthenticationStateProvider _authenticationStateProvider;
+            private readonly IDomainUserRepository _domainUserRepo;
+
+            public Handler(ICommentRepository commentRepo, AuthenticationStateProvider authStateProvider, IDomainUserRepository domainUserRepo)
+            {
+                _commentRepo = commentRepo;
+                _authenticationStateProvider = authStateProvider;
+                _domainUserRepo = domainUserRepo;
+            }
+
+            public async Task<RequestResponse<Response>> Handle(Command request, CancellationToken cancellationToken)
+            {
+                //TODO: user muze byt null, ale to my nechceme!!!
+                var state = await _authenticationStateProvider.GetAuthenticationStateAsync();
+                var id = Guid.Parse(state.User!.FindFirst(ClaimTypes.NameIdentifier)!.Value);
+                var editor = await _domainUserRepo.GetByIdAsync(id);
+                var comment = await _commentRepo.GetByIdAsync(request.commentId);
+
+                if (editor == null)
+                {
+                    return RequestResponse<Response>.CreateErrorResponse($"Editor user with id: {id} not found!");
+                }
+                if (comment == null)
+                {
+                    return RequestResponse<Response>.CreateErrorResponse($"Comment with id: {request.commentId} not found!");
+                }
+                if (editor.UserId != comment.CreatorId)
+                {
+                    return RequestResponse<Response>.CreateErrorResponse($"Editor and comment creator are not the same person!");
+                }
+                if (string.IsNullOrWhiteSpace(request.text))
+                {
+                    return RequestResponse<Response>.CreateErrorResponse($"Comment text is empty!");
+                }
+
+                comment.SetText(request.text);
+
+                if (request.isPrivate)
+                {
+                    comment.SetPrivate();
+                }
+                else
+                {
+                    comment.SetPublic();
+                }
+
+                await _commentRepo.UpdateRepo();
+
+                return RequestResponse<Response>.CreateSuccessResponse(new());
+            }
+        }
+    }
+}
diff --git a/src/Modules/Goal/Application/Leuze.Modules.Goal.Application/CQRS/Comments/Commands/RemoveComment.cs b/src/Modules/Goal/Application/Leuze.Modules.Goal.Application/CQRS/Comments/Commands/RemoveComment.cs
new file mode 100644
index 0000000..c2a2f25
--- /dev/null
+++ b/src/Modules/Goal/Application/Leuze.Modules.Goal.Application/CQRS/Comments/Commands/RemoveComment.cs
@@ -0,0 +1,62 @@
+﻿using Leuze.Core.Application.CQRS;
+using Leuze.Core.Domain.Repositories;
+using Leuze.Modules.Goal.Domain.Repositories;
+using Microsoft.AspNetCore.Components.Authorization;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Security.Claims;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace Leuze.Modules.Goal.Application.CQRS.Comments.Commands
+{
+    public static class RemoveComment
+    {
+        public record Command(Guid commentId) : IBaseRequest<Response>;
+
+        public record Response();
+
+
+        public class Handler : IBaseRequestHandler<Command, Response>
+        {
+            private readonly ICommentRepository _commentRepo;
+            private readonly AuthenticationStateProvider _authenticationStateProvider;
+            private readonly IDomainUserRepository _domainUserRepo;
+
+            public Handler(ICommentRepository commentRepo, AuthenticationStateProvider authStateProvider, IDomainUserRepository domainUserRepo)
+            {
+                _commentRepo = commentRepo;
+                _authenticationStateProvider = authStateProvider;
+                _domainUserRepo = domainUserRepo;
+            }
+
+            public async Task<RequestResponse<Response>> Handle(Command request, CancellationToken cancellationToken)
+            {
+                //TODO: user muze byt null, ale to my nechceme!!!
+                var state = await _authenticationStateProvider.GetAuthenticationStateAsync();
+                var id = Guid.Parse(state.User!.FindFirst(ClaimTypes.NameIdentifier)!.Value);
+                var editor = await _domainUserRepo.GetByIdAsync(id);
+                var comment = await _commentRepo.GetByIdAsync(request.commentId);
+
+                if (editor == null)
+                {
+                    return RequestResponse<Response>.CreateErrorResponse($"Editor user with id: {id} not found!");
+                }
+                if (comment == null)
+                {
+                    return RequestResponse<Response>.CreateErrorResponse($"Comment with id: {request.commentId} not found!");
+                }
+                if (editor.UserId != comment.CreatorId)
+                {
+                    return RequestResponse<Response>.CreateErrorResponse($"Editor and comment creator are not the same person!");
+                }
+
+                await _commentRepo.RemoveAsync(comment.Id);
+
+                return RequestResponse<Response>.CreateSuccessResponse(new());
+            }
+        }
+    }
+}
diff --git a/src/Modules/Goal/Application/Leuze.Modules.Goal.Application/CQRS/Goals/Commands/CreateGoal.cs b/src/Modules/Goal/Application/Leuze.Modules.Goal.Application/CQRS/Goals/Commands/CreateGoal.cs
index 5994a42..f31c93a 100644
--- a/src/Modules/Goal/Application/Leuze.Modules.Goal.Application/CQRS/Goals/Commands/CreateGoal.cs
+++ b/src/Modules/Goal/Application/Leuze.Modules.Goal.Application/CQRS/Goals/Commands/CreateGoal.cs
@@ -51,6 +51,11 @@ namespace Leuze.Modules.Goal.Application.CQRS.Goals.Commands
                     return RequestResponse<Response>.CreateErrorResponse($"Goal definition with id: {request.GoalDefinitionId} not found!");
                 }
 
+                if (goalDef.IsLocked())
+                {
+                    return RequestResponse<Response>.CreateErrorResponse($"Goal definition with id: {request.GoalDefinitionId} is write protected!");
+                }
+
                 GoalItem item = new(goalDef, user, request.Name, request.Description);
                 await _goalItemRepo.AddAsync(item);
 
diff --git a/src/Modules/Goal/Application/Leuze.Modules.Goal.Application/CQRS/Goals/Commands/EditGoal.cs b/src/Modules/Goal/Application/Leuze.Modules.Goal.Application/CQRS/Goals/Commands/EditGoal.cs
index b698193..be2098d 100644
--- a/src/Modules/Goal/Application/Leuze.Modules.Goal.Application/CQRS/Goals/Commands/EditGoal.cs
+++ b/src/Modules/Goal/Application/Leuze.Modules.Goal.Application/CQRS/Goals/Commands/EditGoal.cs
@@ -50,7 +50,17 @@ namespace Leuze.Modules.Goal.Application.CQRS.Goals.Commands
                     return RequestResponse<Response>.CreateErrorResponse($"Cíl s ID: {request.goalId} nebyl nalezen!");
                 }
 
-                if(request.name == null || request.name == "")
+                if (goal.IsLocked())
+                {
+                    return RequestResponse<Response>.CreateErrorResponse($"Cíl s ID: {request.goalId} byl uzamčen proti přepsání!");
+                }
+
+                if (goal.GoalDefinition.IsLocked())
+                {
+                    return RequestResponse<Response>.CreateErrorResponse($"Definice s cílem je uzamčena proti přepsání!");
+                }
+
+                if (request.name == null || request.name == "")
                 {
                     return RequestResponse<Response>.CreateErrorResponse($"Nelze změnit název cíle");
                 }
diff --git a/src/Modules/Goal/Application/Leuze.Modules.Goal.Application/CQRS/Goals/Commands/RemoveGoal.cs b/src/Modules/Goal/Application/Leuze.Modules.Goal.Application/CQRS/Goals/Commands/RemoveGoal.cs
index 06b449b..d60fa24 100644
--- a/src/Modules/Goal/Application/Leuze.Modules.Goal.Application/CQRS/Goals/Commands/RemoveGoal.cs
+++ b/src/Modules/Goal/Application/Leuze.Modules.Goal.Application/CQRS/Goals/Commands/RemoveGoal.cs
@@ -22,6 +22,21 @@ namespace Leuze.Modules.Goal.Application.CQRS.Goals.Commands
 
             public async Task<RequestResponse<Response>> Handle(Command request, CancellationToken cancellationToken)
             {
+                var item = await _repository.GetByIdAsync(request.Id);
+
+                if(item == null)
+                {
+                    return RequestResponse<Response>.CreateErrorResponse($"Goal with ID '{request.Id}' not found");
+                }
+                if (item.IsLocked())
+                {
+                    return RequestResponse<Response>.CreateErrorResponse($"Goal is write protected!");
+                }
+                if (item.GoalDefinition.IsLocked())
+                {
+                    return RequestResponse<Response>.CreateErrorResponse($"Goal definition of item is write protected!");
+                }
+
                 if (!await _repository.RemoveAsync(request.Id))
                     return RequestResponse<Response>.CreateErrorResponse($"Goal with ID '{request.Id}' not found");
 
diff --git a/src/Modules/Goal/Domain/Leuze.Modules.Goal.Domain/Domains/GoalDefinition.cs b/src/Modules/Goal/Domain/Leuze.Modules.Goal.Domain/Domains/GoalDefinition.cs
index 399887e..d036e7f 100644
--- a/src/Modules/Goal/Domain/Leuze.Modules.Goal.Domain/Domains/GoalDefinition.cs
+++ b/src/Modules/Goal/Domain/Leuze.Modules.Goal.Domain/Domains/GoalDefinition.cs
@@ -83,5 +83,10 @@ namespace Leuze.Modules.Goal.Domain.Domains
             this.OwnerLocked = false;
             this.SupervisorLocked = false;
         }
+
+        public bool IsLocked()
+        {
+            return SupervisorLocked && OwnerLocked;
+        }
     }
 }
diff --git a/src/Modules/Goal/Domain/Leuze.Modules.Goal.Domain/Domains/GoalItem.cs b/src/Modules/Goal/Domain/Leuze.Modules.Goal.Domain/Domains/GoalItem.cs
index 1111bbe..8178d38 100644
--- a/src/Modules/Goal/Domain/Leuze.Modules.Goal.Domain/Domains/GoalItem.cs
+++ b/src/Modules/Goal/Domain/Leuze.Modules.Goal.Domain/Domains/GoalItem.cs
@@ -85,5 +85,10 @@ namespace Leuze.Modules.Goal.Domain.Domains
             this.OwnerLocked = false;
             this.SupervisorLocked = false;
         }
+
+        public bool IsLocked()
+        {
+            return SupervisorLocked && OwnerLocked;
+        }
     }
 }
diff --git a/src/Modules/Goal/Infrastructure/Leuze.Modules.Goal.Infrastructure.Persistence/Repositories/GoalItemRepository.cs b/src/Modules/Goal/Infrastructure/Leuze.Modules.Goal.Infrastructure.Persistence/Repositories/GoalItemRepository.cs
index 28b7073..11af915 100644
--- a/src/Modules/Goal/Infrastructure/Leuze.Modules.Goal.Infrastructure.Persistence/Repositories/GoalItemRepository.cs
+++ b/src/Modules/Goal/Infrastructure/Leuze.Modules.Goal.Infrastructure.Persistence/Repositories/GoalItemRepository.cs
@@ -38,7 +38,7 @@ namespace Leuze.Modules.Goal.Infrastructure.Persistence.Repositories
 
         public async Task<GoalItem> GetByIdAsync(Guid id)
         {
-            return await dbSet.FindAsync(id);
+            return await dbSet.Where(o => o.Id == id).Include(o => o.GoalDefinition).FirstOrDefaultAsync();
         }
 
         public async Task<List<GoalItem>> GetAllForUserDefinitionAsync(Guid definitionId)
