Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 6013887a

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

Repo OK

Zobrazit rozdíly:

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

  
14
namespace Leuze.Modules.Goal.Application.CQRS.Goals.Commands
15
{
16
    public static class CreateGoalDefinition
17
    {
18
        public record Command(Guid GoalDefinitionId, string Name, string Description) : IBaseRequest<Response>;
19

  
20
        public record Response(GoalItem item);
21

  
22

  
23
        public class Handler : IBaseRequestHandler<Command, Response>
24
        {
25
            private readonly IGoalItemRepository _goalItemRepo;
26
            private readonly IGoalDefinitionRepository _goalDefinitionRepo;
27
            private readonly AuthenticationStateProvider _authenticationStateProvider;
28
            private readonly IDomainUserRepository _domainUserRepo;
29

  
30
            public Handler(IGoalItemRepository goalItemRepo, IGoalDefinitionRepository goalDefinitionRepo, AuthenticationStateProvider authStateProvider, IDomainUserRepository domainUserRepo)
31
            {
32
                _goalItemRepo = goalItemRepo;
33
                _goalDefinitionRepo = goalDefinitionRepo;
34
                _authenticationStateProvider = authStateProvider;
35
                _domainUserRepo = domainUserRepo;
36
            }
37

  
38
            public async Task<RequestResponse<Response>> Handle(Command request, CancellationToken cancellationToken)
39
            {
40
                //TODO: user muze byt null, ale to my nechceme!!!
41
                var state = await _authenticationStateProvider.GetAuthenticationStateAsync();
42
                var id = Guid.Parse(state.User!.FindFirst(ClaimTypes.NameIdentifier)!.Value);
43
                var user = await _domainUserRepo.GetByIdAsync(id);
44
                var goalDef = await _goalDefinitionRepo.GetByIdAsync(request.GoalDefinitionId);
45

  
46
                if(goalDef == null)
47
                {
48
                    return RequestResponse<Response>.CreateErrorResponse($"Goal definition with id: {request.GoalDefinitionId} not found!");
49
                }
50

  
51
                GoalItem item = new(goalDef, user, request.Name, request.Description);
52

  
53
                await _goalItemRepo.AddAsync(item);
54

  
55
                return RequestResponse<Response>.CreateSuccessResponse(new(item));
56
            }
57
        }
58

  
59
    }
60
}
src/Modules/Goal/Application/Leuze.Modules.Goal.Application/CQRS/GoalDefinitions/Queries/GetGoalDefinitionById.cs
1
using Leuze.Core.Application.CQRS;
2
using Leuze.Core.Domain.Repositories;
3
using Leuze.Modules.Goal.Domain.Domains;
4
using Leuze.Modules.Goal.Domain.Repositories;
5
using Microsoft.AspNetCore.Components.Authorization;
6
using System;
7
using System.Collections.Generic;
8
using System.Linq;
9
using System.Security.Claims;
10
using System.Text;
11
using System.Threading;
12
using System.Threading.Tasks;
13

  
14
namespace Leuze.Modules.Goal.Application.CQRS.Goals.Commands
15
{
16
    public static class GetGoalDefinitionById
17
    {
18
        public record Command(Guid GoalDefinitionId, string Name, string Description) : IBaseRequest<Response>;
19

  
20
        public record Response(GoalItem item);
21

  
22

  
23
        public class Handler : IBaseRequestHandler<Command, Response>
24
        {
25
            private readonly IGoalItemRepository _goalItemRepo;
26
            private readonly IGoalDefinitionRepository _goalDefinitionRepo;
27
            private readonly AuthenticationStateProvider _authenticationStateProvider;
28
            private readonly IDomainUserRepository _domainUserRepo;
29

  
30
            public Handler(IGoalItemRepository goalItemRepo, IGoalDefinitionRepository goalDefinitionRepo, AuthenticationStateProvider authStateProvider, IDomainUserRepository domainUserRepo)
31
            {
32
                _goalItemRepo = goalItemRepo;
33
                _goalDefinitionRepo = goalDefinitionRepo;
34
                _authenticationStateProvider = authStateProvider;
35
                _domainUserRepo = domainUserRepo;
36
            }
37

  
38
            public async Task<RequestResponse<Response>> Handle(Command request, CancellationToken cancellationToken)
39
            {
40
                //TODO: user muze byt null, ale to my nechceme!!!
41
                var state = await _authenticationStateProvider.GetAuthenticationStateAsync();
42
                var id = Guid.Parse(state.User!.FindFirst(ClaimTypes.NameIdentifier)!.Value);
43
                var user = await _domainUserRepo.GetByIdAsync(id);
44
                var goalDef = await _goalDefinitionRepo.GetByIdAsync(request.GoalDefinitionId);
45

  
46
                if(goalDef == null)
47
                {
48
                    return RequestResponse<Response>.CreateErrorResponse($"Goal definition with id: {request.GoalDefinitionId} not found!");
49
                }
50

  
51
                GoalItem item = new(goalDef, user, request.Name, request.Description);
52

  
53
                await _goalItemRepo.AddAsync(item);
54

  
55
                return RequestResponse<Response>.CreateSuccessResponse(new(item));
56
            }
57
        }
58

  
59
    }
60
}
src/Modules/Goal/Application/Leuze.Modules.Goal.Application/CQRS/Goals/Commands/CreateGoal.cs
1 1
using Leuze.Core.Application.CQRS;
2
using Leuze.Core.Domain.Domains.Users.Services.Abstractions;
2
using Leuze.Core.Domain.Repositories;
3 3
using Leuze.Modules.Goal.Domain.Domains;
4 4
using Leuze.Modules.Goal.Domain.Repositories;
5
using Microsoft.AspNetCore.Components.Authorization;
5 6
using System;
6 7
using System.Collections.Generic;
7 8
using System.Linq;
9
using System.Security.Claims;
8 10
using System.Text;
9 11
using System.Threading;
10 12
using System.Threading.Tasks;
......
22 24
        {
23 25
            private readonly IGoalItemRepository _goalItemRepo;
24 26
            private readonly IGoalDefinitionRepository _goalDefinitionRepo;
25
            private readonly IDomainUserProvider _domainUserProvider;
27
            private readonly AuthenticationStateProvider _authenticationStateProvider;
28
            private readonly IDomainUserRepository _domainUserRepo;
26 29

  
27
            public Handler(IGoalItemRepository goalItemRepo, IGoalDefinitionRepository goalDefinitionRepo, IDomainUserProvider domainuserProvider)
30
            public Handler(IGoalItemRepository goalItemRepo, IGoalDefinitionRepository goalDefinitionRepo, AuthenticationStateProvider authStateProvider, IDomainUserRepository domainUserRepo)
28 31
            {
29 32
                _goalItemRepo = goalItemRepo;
30 33
                _goalDefinitionRepo = goalDefinitionRepo;
31
                _domainUserProvider = domainuserProvider;
34
                _authenticationStateProvider = authStateProvider;
35
                _domainUserRepo = domainUserRepo;
32 36
            }
33 37

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

  
40 45
                if(goalDef == null)
src/Modules/Goal/Application/Leuze.Modules.Goal.Application/CQRS/Goals/Queries/GetGoalById.cs
1
using Leuze.Core.Application.CQRS;
2
using Leuze.Core.Domain.Repositories;
3
using Leuze.Modules.Goal.Domain.Domains;
4
using Leuze.Modules.Goal.Domain.Repositories;
5
using Microsoft.AspNetCore.Components.Authorization;
6
using System;
7
using System.Collections.Generic;
8
using System.Linq;
9
using System.Security.Claims;
10
using System.Text;
11
using System.Threading;
12
using System.Threading.Tasks;
13

  
14
namespace Leuze.Modules.Goal.Application.CQRS.Goals.Commands
15
{
16
    public static class GetGoalById
17
    {
18
        public record Command(Guid GoalDefinitionId, string Name, string Description) : IBaseRequest<Response>;
19

  
20
        public record Response(GoalItem item);
21

  
22

  
23
        public class Handler : IBaseRequestHandler<Command, Response>
24
        {
25
            private readonly IGoalItemRepository _goalItemRepo;
26
            private readonly IGoalDefinitionRepository _goalDefinitionRepo;
27
            private readonly AuthenticationStateProvider _authenticationStateProvider;
28
            private readonly IDomainUserRepository _domainUserRepo;
29

  
30
            public Handler(IGoalItemRepository goalItemRepo, IGoalDefinitionRepository goalDefinitionRepo, AuthenticationStateProvider authStateProvider, IDomainUserRepository domainUserRepo)
31
            {
32
                _goalItemRepo = goalItemRepo;
33
                _goalDefinitionRepo = goalDefinitionRepo;
34
                _authenticationStateProvider = authStateProvider;
35
                _domainUserRepo = domainUserRepo;
36
            }
37

  
38
            public async Task<RequestResponse<Response>> Handle(Command request, CancellationToken cancellationToken)
39
            {
40
                //TODO: user muze byt null, ale to my nechceme!!!
41
                var state = await _authenticationStateProvider.GetAuthenticationStateAsync();
42
                var id = Guid.Parse(state.User!.FindFirst(ClaimTypes.NameIdentifier)!.Value);
43
                var user = await _domainUserRepo.GetByIdAsync(id);
44
                var goalDef = await _goalDefinitionRepo.GetByIdAsync(request.GoalDefinitionId);
45

  
46
                if(goalDef == null)
47
                {
48
                    return RequestResponse<Response>.CreateErrorResponse($"Goal definition with id: {request.GoalDefinitionId} not found!");
49
                }
50

  
51
                GoalItem item = new(goalDef, user, request.Name, request.Description);
52

  
53
                await _goalItemRepo.AddAsync(item);
54

  
55
                return RequestResponse<Response>.CreateSuccessResponse(new(item));
56
            }
57
        }
58

  
59
    }
60
}
src/Modules/Goal/Domain/Leuze.Modules.Goal.Domain/Domains/Comment.cs
30 30
        public Guid CreatorId { get; private set; }
31 31
        public GoalItem Goal { get; private set; }
32 32
        public Guid GoalId { get; private set; }
33
        public string Text { get; private set; }
33
        public string Text { get; set; }
34 34
        public bool IsPrivate { get; private set; }
35
        public bool IsChanged { get; private set; }
35
        public bool IsChanged { get; set; }
36 36
    }
37 37
}
src/Modules/Goal/Domain/Leuze.Modules.Goal.Domain/Domains/GoalDefinition.cs
32 32
        public Guid OwnerId { get; private set; }
33 33
        public DomainUser Creator { get; private set; }
34 34
        public Guid CreatorId { get; private set; }
35
        public string Name { get; private set; }
36
        public DateTime From { get; private set; }
37
        public DateTime To { get; private set; }
35
        public string Name { get; set; }
36
        public DateTime From { get; set; }
37
        public DateTime To { get; set; }
38 38
        public bool OwnerLocked { get; private set; }
39 39
        public bool SupervisorLocked { get; private set; }
40
        public decimal? Vari { get; private set; }
40
        public decimal? Vari { get; set; }
41 41

  
42 42
        private List<GoalItem> _goals { get; set; }
43 43
        public IReadOnlyList<GoalItem> Goals => _goals.AsReadOnly();
src/Modules/Goal/Domain/Leuze.Modules.Goal.Domain/Domains/GoalItem.cs
30 30
        public Guid GoalDefinitionId { get; private set; }
31 31
        public DomainUser Creator { get; private set; }
32 32
        public Guid CreatorId { get; private set; }
33
        public string Name { get; private set; }
34
        public string Description { get; private set; }
35
        public int PercentileUser { get; private set; }
36
        public int PercentileFinal { get; private set; }
33
        public string Name { get; set; }
34
        public string Description { get; set; }
35
        public decimal PercentileUser { get; set; }
36
        public decimal PercentileFinal { get; set; }
37 37

  
38 38
        private List<Comment> _comments { get; set; }
39 39
        public IReadOnlyList<Comment> Comments => _comments.AsReadOnly();
src/Modules/Goal/Domain/Leuze.Modules.Goal.Domain/Repositories/IReviewRepository.cs
16 16

  
17 17
        Task<Review> GetByIdAsync(Guid id);
18 18

  
19
        Task<List<Review>> GetAllForGoalAsync(Guid definitionId);
19
        Task<List<Review>> GetAllForGoalAsync(Guid goalId);
20 20

  
21 21
        Task<List<Review>> GetAllAsync();
22 22
    }
src/Modules/Goal/Infrastructure/Leuze.Modules.Goal.Infrastructure.Persistence/Repositories/CommentRepository.cs
16 16
        public CommentRepository(IStorageContext context, IMapper mapper) : base(context, mapper)
17 17
        {
18 18
        }
19

  
20
        public async Task<bool> AddAsync(Comment comment)
21
        {
22
            await dbSet.AddAsync(comment);
23
            await storageContext.SaveChangesAsync();
24
            return true;
25
        }
26

  
27
        public async Task<bool> RemoveAsync(Guid id)
28
        {
29
            Comment item = await GetByIdAsync(id);
30
            if (item == null)
31
            {
32
                return false;
33
            }
34
            dbSet.Remove(item);
35
            await storageContext.SaveChangesAsync();
36
            return true;
37
        }
38

  
39
        public async Task<Comment> GetByIdAsync(Guid id)
40
        {
41
            return await dbSet.FindAsync(id);
42
        }
43

  
44
        public async Task<List<Comment>> GetAllForGoalAsync(Guid goalId)
45
        {
46
            return dbSet.Where(item => item.GoalId == goalId).ToList(); //TODO: await??
47
        }
48

  
49
        public async Task<List<Comment>> GetAllAsync()
50
        {
51
            return dbSet.ToList(); //TODO: Await??
52
        }
53

  
54
        public async Task<bool> EditAsync(Guid id, string text)
55
        {
56
            Comment item = await dbSet.FindAsync(id);
57
            if (item == null) return false;
58
            item.Text = text;
59
            item.IsChanged = true;
60
            await storageContext.SaveChangesAsync();
61
            return true;
62
        }
19 63
    }
20 64
}
src/Modules/Goal/Infrastructure/Leuze.Modules.Goal.Infrastructure.Persistence/Repositories/GoalDefinitionRepository.cs
19 19
        {
20 20
        }
21 21

  
22
        public async Task<bool> AddAsync(GoalDefinition gd)
23
        {
24
            await dbSet.AddAsync(gd);
25
            await storageContext.SaveChangesAsync();
26
            return true;
27
        }
28

  
29
        public async Task<bool> RemoveAsync(Guid id)
30
        {
31
            GoalDefinition item = await GetByIdAsync(id);
32
            if (item == null)
33
            {
34
                return false;
35
            }
36
            dbSet.Remove(item);
37
            await storageContext.SaveChangesAsync();
38
            return true;
39
        }
40

  
41
        public async Task<List<GoalDefinition>> GetAllForUserAsync(Guid userId)
42
        {
43
            return dbSet.Where(item => item.OwnerId == userId).ToList(); //TODO: await??
44
        }
45

  
46
        public async Task<List<GoalDefinition>> GetAllAsync()
47
        {
48
            return dbSet.ToList(); //TODO: Await??
49
        }
50

  
22 51
        public async Task<GoalDefinition> GetByIdAsync(Guid id)
23 52
        {
24
            return await dbSet.Where(item => item.Id == id).SingleOrDefaultAsync();
53
            //return await dbSet.Where(item => item.Id == id).SingleOrDefaultAsync();
54
            return await dbSet.FindAsync(id);
55
        }
56

  
57
        public async Task<bool> SetVariAsync(Guid id, decimal vari)
58
        {
59
            GoalDefinition item = await dbSet.FindAsync(id);
60
            if (item == null) return false;
61
            item.Vari = vari;
62
            await storageContext.SaveChangesAsync();
63
            return true;
64
        }
65

  
66
        public async Task<bool> SetFromAsync(Guid id, DateTime from)
67
        {
68
            GoalDefinition item = await dbSet.FindAsync(id);
69
            if (item == null) return false;
70
            item.From = from;
71
            await storageContext.SaveChangesAsync();
72
            return true;
73
        }
74

  
75
        public async Task<bool> SetToAsync(Guid id, DateTime to)
76
        {
77
            GoalDefinition item = await dbSet.FindAsync(id);
78
            if (item == null) return false;
79
            item.To = to;
80
            await storageContext.SaveChangesAsync();
81
            return true;
82
        }
83

  
84
        public async Task<bool> SetNameAsync(Guid id, string name)
85
        {
86
            GoalDefinition item = await dbSet.FindAsync(id);
87
            if (item == null) return false;
88
            item.Name = name;
89
            await storageContext.SaveChangesAsync();
90
            return true;
25 91
        }
26 92
    }
27 93
}
src/Modules/Goal/Infrastructure/Leuze.Modules.Goal.Infrastructure.Persistence/Repositories/GoalItemRepository.cs
22 22
            await dbSet.AddAsync(item);
23 23
            await storageContext.SaveChangesAsync();
24 24
        }
25

  
26
        public async Task<bool> RemoveAsync(Guid id)
27
        {
28
            GoalItem item = await GetByIdAsync(id);
29
            if(item == null)
30
            {
31
                return false;
32
            }
33
            dbSet.Remove(item);
34
            await storageContext.SaveChangesAsync();
35
            return true;
36
        }
37

  
38
        public async Task<GoalItem> GetByIdAsync(Guid id)
39
        {
40
            return await dbSet.FindAsync(id);
41
        }
42

  
43
        public async Task<List<GoalItem>> GetAllForUserDefinitionAsync(Guid definitionId)
44
        {
45
            return dbSet.Where(item => item.GoalDefinitionId == definitionId).ToList(); //TODO: await??
46
        }
47

  
48
        public async Task<List<GoalItem>> GetAllAsync()
49
        {
50
            return dbSet.ToList(); //TODO: Await??
51
        }
52

  
53
        public async Task<bool> SetNameAsync(Guid id, string name)
54
        {
55
            GoalItem item = await dbSet.FindAsync(id);
56
            if(item == null) return false;
57
            item.Name = name;
58
            await storageContext.SaveChangesAsync();
59
            return true;
60
        }
61

  
62
        public async Task<bool> SetDescriptionAsync(Guid id, string description)
63
        {
64
            GoalItem item = await dbSet.FindAsync(id);
65
            if (item == null) return false;
66
            item.Description = description;
67
            await storageContext.SaveChangesAsync();
68
            return true;
69
        }
70

  
71
        public async Task<bool> SetPercentileUserAsync(Guid id, decimal percentile)
72
        {
73
            GoalItem item = await dbSet.FindAsync(id);
74
            if (item == null) return false;
75
            item.PercentileUser = percentile;
76
            await storageContext.SaveChangesAsync();
77
            return true;
78
        }
79

  
80
        public async Task<bool> SetPercentileFinalAsync(Guid id, decimal percentile)
81
        {
82
            GoalItem item = await dbSet.FindAsync(id);
83
            if (item == null) return false;
84
            item.PercentileFinal = percentile;
85
            await storageContext.SaveChangesAsync();
86
            return true;
87
        }
88

  
89
        public async Task<bool> SetGoalDefinitionAsync(Guid id, Guid definitionId)
90
        {
91
            return false; //TODO: Not needed now, hope not
92
        }
25 93
    }
26 94
}
src/Modules/Goal/Infrastructure/Leuze.Modules.Goal.Infrastructure.Persistence/Repositories/GoalUpdateRepository.cs
16 16
        public GoalUpdateRepository(IStorageContext context, IMapper mapper) : base(context, mapper)
17 17
        {
18 18
        }
19

  
20
        public async Task<bool> AddAsync(GoalUpdate update)
21
        {
22
            await dbSet.AddAsync(update);
23
            await storageContext.SaveChangesAsync();
24
            return true;
25
        }
26

  
27
        public async Task<bool> RemoveAsync(Guid id)
28
        {
29
            GoalUpdate item = await GetByIdAsync(id);
30
            if (item == null)
31
            {
32
                return false;
33
            }
34
            dbSet.Remove(item);
35
            await storageContext.SaveChangesAsync();
36
            return true;
37
        }
38

  
39
        public async Task<GoalUpdate> GetByIdAsync(Guid id)
40
        {
41
            return await dbSet.FindAsync(id);
42
        }
43

  
44
        public async Task<List<GoalUpdate>> GetAllForGoalAsync(Guid goalId)
45
        {
46
            return dbSet.Where(item => item.GoalId == goalId).ToList(); //TODO: await??
47
        }
48

  
49
        public async Task<List<GoalUpdate>> GetAllAsync()
50
        {
51
            return dbSet.ToList(); //TODO: Await??
52
        }
19 53
    }
20 54
}
src/Modules/Goal/Infrastructure/Leuze.Modules.Goal.Infrastructure.Persistence/Repositories/ReviewRepository.cs
16 16
        public ReviewRepository(IStorageContext context, IMapper mapper) : base(context, mapper)
17 17
        {
18 18
        }
19

  
20
        public async Task<bool> AddAsync(Review review)
21
        {
22
            await dbSet.AddAsync(review);
23
            await storageContext.SaveChangesAsync();
24
            return true;
25
        }
26

  
27
        public async Task<bool> RemoveAsync(Guid id)
28
        {
29
            Review item = await GetByIdAsync(id);
30
            if (item == null)
31
            {
32
                return false;
33
            }
34
            dbSet.Remove(item);
35
            await storageContext.SaveChangesAsync();
36
            return true;
37
        }
38

  
39
        public async Task<Review> GetByIdAsync(Guid id)
40
        {
41
            return await dbSet.FindAsync(id);
42
        }
43

  
44
        public async Task<List<Review>> GetAllForGoalAsync(Guid goalId)
45
        {
46
            return dbSet.Where(item => item.GoalId == goalId).ToList(); //TODO: await??
47
        }
48

  
49
        public async Task<List<Review>> GetAllAsync()
50
        {
51
            return dbSet.ToList(); //TODO: Await??
52
        }
19 53
    }
20 54
}

Také k dispozici: Unified diff