-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/issue 65 final: CRUD endpoints for Programs and Program categories #320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 16 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
7c42c1e
updated database seeder
ZhmudAnastasiia d9f6798
fix integrational tests
MotrukOleg dd10411
move to in memory database and added possibility to add custom seeder…
MotrukOleg 53e437c
Added InMemoryDbContext, fixed integration tests
daniilshevch 1455f8d
fix naming
MotrukOleg 6ec55bd
fix merge conf
MotrukOleg 4fac8ca
created models
OlyaMraka e9a38ec
Added commands, handler, queries, tests
OlyaMraka 2942c71
migration
OlyaMraka 4ed27c4
resolve merge conf
MotrukOleg a2d583c
fix image integration tests
MotrukOleg 299e967
Merge branch 'feature/issue-168' into feature/issue-65-final
OlyaMraka c9ec121
Added tests
OlyaMraka 69f167c
Added GetByFilters, fixed bugs
OlyaMraka 5db32ae
little fixes
OlyaMraka c21d3bc
fix integration tests
MotrukOleg 5f358bd
minor fixes
OlyaMraka 3d403f1
fixed SonarQube issues
OlyaMraka 4ae81ff
Merge branch 'release/1.0.0' into feature/issue-65-final
OlyaMraka cba9772
fixed merge conflicts
OlyaMraka 7b84c41
minor fixes
OlyaMraka cd83347
fixed SonarQube issues
OlyaMraka c8ee5e0
SonarQube fixies
OlyaMraka 74acdde
moved id from body to route
OlyaMraka 7125cc0
Merge branch 'release/1.0.0' into feature/issue-65-final
OlyaMraka 6494676
minor fixes
OlyaMraka 2249125
fixed comments
Kitukl 796deb5
minor fixes
Kitukl 20c250e
fixed comments
Kitukl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
...enter/VictoryCenter.BLL/Commands/ProgramCategories/Create/CreateProgramCategoryCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| using FluentResults; | ||
| using MediatR; | ||
| using VictoryCenter.BLL.DTOs.ProgramCategories; | ||
|
|
||
| namespace VictoryCenter.BLL.Commands.ProgramCategories.Create; | ||
|
|
||
| public record CreateProgramCategoryCommand(CreateProgramCategoryDto programCategoryDto) | ||
| : IRequest<Result<ProgramCategoryDto>>; |
47 changes: 47 additions & 0 deletions
47
...enter/VictoryCenter.BLL/Commands/ProgramCategories/Create/CreateProgramCategoryHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| using MediatR; | ||
| using AutoMapper; | ||
| using FluentResults; | ||
| using FluentValidation; | ||
| using VictoryCenter.BLL.Constants; | ||
| using VictoryCenter.BLL.DTOs.ProgramCategories; | ||
| using VictoryCenter.DAL.Repositories.Interfaces.Base; | ||
|
|
||
| namespace VictoryCenter.BLL.Commands.ProgramCategories.Create; | ||
|
|
||
| public class CreateProgramCategoryHandler : IRequestHandler<CreateProgramCategoryCommand, Result<ProgramCategoryDto>> | ||
| { | ||
| private readonly IMapper _mapper; | ||
| private readonly IRepositoryWrapper _repositoryWrapper; | ||
| private readonly IValidator<CreateProgramCategoryCommand> _validator; | ||
|
|
||
| public CreateProgramCategoryHandler(IMapper mapper, IRepositoryWrapper repositoryWrapper, IValidator<CreateProgramCategoryCommand> validator) | ||
| { | ||
| _mapper = mapper; | ||
| _repositoryWrapper = repositoryWrapper; | ||
| _validator = validator; | ||
| } | ||
|
|
||
| public async Task<Result<ProgramCategoryDto>> Handle(CreateProgramCategoryCommand request, CancellationToken cancellationToken) | ||
| { | ||
| try | ||
| { | ||
| await _validator.ValidateAndThrowAsync(request, cancellationToken); | ||
|
|
||
| var entity = _mapper.Map<DAL.Entities.ProgramCategory>(request.programCategoryDto); | ||
| entity.CreatedAt = DateTime.UtcNow; | ||
| await _repositoryWrapper.ProgramCategoriesRepository.CreateAsync(entity); | ||
|
|
||
| if (await _repositoryWrapper.SaveChangesAsync() > 0) | ||
| { | ||
| var responseDto = _mapper.Map<ProgramCategoryDto>(entity); | ||
| return Result.Ok(responseDto); | ||
| } | ||
|
|
||
| return Result.Fail<ProgramCategoryDto>(ProgramCategoryConstants.FailedToCreateCategory); | ||
| } | ||
| catch (ValidationException ex) | ||
| { | ||
| return Result.Fail<ProgramCategoryDto>(ex.Message); | ||
| } | ||
| } | ||
| } | ||
6 changes: 6 additions & 0 deletions
6
...enter/VictoryCenter.BLL/Commands/ProgramCategories/Delete/DeleteProgramCategoryCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| using MediatR; | ||
| using FluentResults; | ||
|
|
||
| namespace VictoryCenter.BLL.Commands.ProgramCategories.Delete; | ||
|
|
||
| public record DeleteProgramCategoryCommand(long id) : IRequest<Result<long>>; |
51 changes: 51 additions & 0 deletions
51
...enter/VictoryCenter.BLL/Commands/ProgramCategories/Delete/DeleteProgramCategoryHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| using MediatR; | ||
| using FluentResults; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using VictoryCenter.BLL.Constants; | ||
| using VictoryCenter.DAL.Repositories.Options; | ||
| using VictoryCenter.DAL.Repositories.Interfaces.Base; | ||
|
|
||
| namespace VictoryCenter.BLL.Commands.ProgramCategories.Delete; | ||
|
|
||
| public class DeleteProgramCategoryHandler : IRequestHandler<DeleteProgramCategoryCommand, Result<long>> | ||
| { | ||
| private readonly IRepositoryWrapper _repositoryWrapper; | ||
|
|
||
| public DeleteProgramCategoryHandler(IRepositoryWrapper repositoryWrapper) | ||
| { | ||
| _repositoryWrapper = repositoryWrapper; | ||
| } | ||
|
|
||
| public async Task<Result<long>> Handle(DeleteProgramCategoryCommand request, CancellationToken cancellationToken) | ||
|
Kitukl marked this conversation as resolved.
|
||
| { | ||
| var queryOptions = new QueryOptions<DAL.Entities.ProgramCategory> | ||
| { | ||
| Filter = programCategory => programCategory.Id == request.id, | ||
| Include = programCategory => programCategory | ||
| .Include(p => p.Programs) | ||
| }; | ||
|
|
||
| var entityToDelete = await _repositoryWrapper.ProgramCategoriesRepository | ||
| .GetFirstOrDefaultAsync(queryOptions); | ||
|
|
||
| if (entityToDelete is null) | ||
| { | ||
| return Result.Fail<long>(ErrorMessagesConstants | ||
| .NotFound(request.id, typeof(DAL.Entities.ProgramCategory))); | ||
| } | ||
|
|
||
| if (entityToDelete.Programs.Count != 0) | ||
| { | ||
| return Result.Fail(ProgramCategoryConstants.CantDeleteProgramCategoryWhileAssociatedWithAnyProgram); | ||
| } | ||
|
|
||
| _repositoryWrapper.ProgramCategoriesRepository.Delete(entityToDelete); | ||
|
|
||
| if (await _repositoryWrapper.SaveChangesAsync() > 0) | ||
| { | ||
| return Result.Ok(entityToDelete.Id); | ||
| } | ||
|
|
||
| return Result.Fail(ProgramCategoryConstants.FailedToDeleteCategory); | ||
| } | ||
| } | ||
8 changes: 8 additions & 0 deletions
8
...enter/VictoryCenter.BLL/Commands/ProgramCategories/Update/UpdateProgramCategoryCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| using FluentResults; | ||
| using MediatR; | ||
| using VictoryCenter.BLL.DTOs.ProgramCategories; | ||
|
|
||
| namespace VictoryCenter.BLL.Commands.ProgramCategories.Update; | ||
|
|
||
| public record UpdateProgramCategoryCommand(UpdateProgramCategoryDto updateProgramCategoryDto) | ||
| : IRequest<Result<ProgramCategoryDto>>; |
61 changes: 61 additions & 0 deletions
61
...enter/VictoryCenter.BLL/Commands/ProgramCategories/Update/UpdateProgramCategoryHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| using MediatR; | ||
| using AutoMapper; | ||
| using FluentResults; | ||
| using FluentValidation; | ||
| using VictoryCenter.BLL.Constants; | ||
| using VictoryCenter.BLL.DTOs.ProgramCategories; | ||
| using VictoryCenter.DAL.Repositories.Options; | ||
| using VictoryCenter.DAL.Repositories.Interfaces.Base; | ||
|
|
||
| namespace VictoryCenter.BLL.Commands.ProgramCategories.Update; | ||
|
|
||
| public class UpdateProgramCategoryHandler : IRequestHandler<UpdateProgramCategoryCommand, Result<ProgramCategoryDto>> | ||
| { | ||
| private readonly IMapper _mapper; | ||
| private readonly IRepositoryWrapper _repositoryWrapper; | ||
| private readonly IValidator<UpdateProgramCategoryCommand> _validator; | ||
|
|
||
| public UpdateProgramCategoryHandler(IMapper mapper, IRepositoryWrapper repositoryWrapper, IValidator<UpdateProgramCategoryCommand> validator) | ||
| { | ||
| _mapper = mapper; | ||
| _repositoryWrapper = repositoryWrapper; | ||
| _validator = validator; | ||
| } | ||
|
|
||
| public async Task<Result<ProgramCategoryDto>> Handle(UpdateProgramCategoryCommand request, CancellationToken cancellationToken) | ||
| { | ||
| try | ||
| { | ||
| await _validator.ValidateAndThrowAsync(request, cancellationToken); | ||
|
|
||
| var programCategoryEntity = await _repositoryWrapper.ProgramCategoriesRepository | ||
| .GetFirstOrDefaultAsync(new QueryOptions<DAL.Entities.ProgramCategory> | ||
| { | ||
| Filter = programCategory => programCategory.Id == request.updateProgramCategoryDto.Id | ||
| }); | ||
|
|
||
| if (programCategoryEntity is null) | ||
| { | ||
| return Result.Fail<ProgramCategoryDto>(ErrorMessagesConstants | ||
| .NotFound(request.updateProgramCategoryDto.Id, typeof(DAL.Entities.ProgramCategory))); | ||
| } | ||
|
|
||
| var entityToUpdate = _mapper.Map(request.updateProgramCategoryDto, programCategoryEntity); | ||
| entityToUpdate.CreatedAt = programCategoryEntity.CreatedAt; | ||
|
|
||
| _repositoryWrapper.ProgramCategoriesRepository.Update(entityToUpdate); | ||
|
|
||
| if (await _repositoryWrapper.SaveChangesAsync() > 0) | ||
| { | ||
| var responseDto = _mapper.Map<ProgramCategoryDto>(entityToUpdate); | ||
| return Result.Ok(responseDto); | ||
| } | ||
|
|
||
| return Result.Fail<ProgramCategoryDto>(ProgramCategoryConstants.FailedToUpdateCategory); | ||
| } | ||
| catch (ValidationException ex) | ||
| { | ||
| return Result.Fail<ProgramCategoryDto>(ex.Message); | ||
|
Kitukl marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
| } | ||
7 changes: 7 additions & 0 deletions
7
VictoryCenter/VictoryCenter.BLL/Commands/Programs/Create/CreateProgramCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| using MediatR; | ||
| using FluentResults; | ||
| using VictoryCenter.BLL.DTOs.Programs; | ||
|
|
||
| namespace VictoryCenter.BLL.Commands.Programs.Create; | ||
|
|
||
| public record CreateProgramCommand(CreateProgramDto createProgramDto) : IRequest<Result<ProgramDto>>; |
85 changes: 85 additions & 0 deletions
85
VictoryCenter/VictoryCenter.BLL/Commands/Programs/Create/CreateProgramHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| using MediatR; | ||
| using AutoMapper; | ||
| using FluentResults; | ||
| using FluentValidation; | ||
| using VictoryCenter.BLL.Constants; | ||
| using VictoryCenter.BLL.DTOs.Programs; | ||
| using VictoryCenter.BLL.Interfaces.BlobStorage; | ||
| using VictoryCenter.DAL.Entities; | ||
| using VictoryCenter.DAL.Repositories.Interfaces.Base; | ||
| using VictoryCenter.DAL.Repositories.Options; | ||
|
|
||
| namespace VictoryCenter.BLL.Commands.Programs.Create; | ||
|
|
||
| public class CreateProgramHandler : IRequestHandler<CreateProgramCommand, Result<ProgramDto>> | ||
| { | ||
| private readonly IMapper _mapper; | ||
| private readonly IRepositoryWrapper _repositoryWrapper; | ||
| private readonly IValidator<CreateProgramCommand> _validator; | ||
| private readonly IBlobService _blobService; | ||
|
|
||
| public CreateProgramHandler(IMapper mapper, IRepositoryWrapper repositoryWrapper, IValidator<CreateProgramCommand> validator, IBlobService blobService) | ||
| { | ||
| _mapper = mapper; | ||
| _repositoryWrapper = repositoryWrapper; | ||
| _validator = validator; | ||
| _blobService = blobService; | ||
| } | ||
|
|
||
| public async Task<Result<ProgramDto>> Handle(CreateProgramCommand request, CancellationToken cancellationToken) | ||
| { | ||
| try | ||
| { | ||
| await _validator.ValidateAndThrowAsync(request, cancellationToken); | ||
|
|
||
| var categoryOptions = new QueryOptions<ProgramCategory> | ||
|
Kitukl marked this conversation as resolved.
Outdated
|
||
| { | ||
| Filter = category => request.createProgramDto.CategoriesId.Contains(category.Id), | ||
| AsNoTracking = false | ||
| }; | ||
|
|
||
| var categories = await _repositoryWrapper | ||
| .ProgramCategoriesRepository.GetAllAsync(categoryOptions); | ||
|
|
||
| var entity = _mapper.Map<Program>(request.createProgramDto); | ||
|
|
||
| if (entity.ImageId != null) | ||
| { | ||
| var newImage = await _repositoryWrapper.ImageRepository.GetFirstOrDefaultAsync(new QueryOptions<Image> | ||
| { | ||
| Filter = image => image.Id == request.createProgramDto.ImageId, | ||
| AsNoTracking = false | ||
| }); | ||
| if (newImage is not null) | ||
| { | ||
| try | ||
|
Kitukl marked this conversation as resolved.
Outdated
|
||
| { | ||
| newImage.Base64 = await _blobService.FindFileInStorageAsBase64Async(newImage.BlobName, newImage.MimeType); | ||
| } | ||
| catch(Exception) | ||
| { | ||
| return Result.Fail<ProgramDto>(ProgramConstants.FailedRetrievingProgramPhoto); | ||
| } | ||
| } | ||
|
|
||
| entity.Image = newImage; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| entity.Categories = categories.ToList(); | ||
| entity.CreatedAt = DateTime.UtcNow; | ||
|
|
||
| await _repositoryWrapper.ProgramsRepository.CreateAsync(entity); | ||
|
|
||
| if (await _repositoryWrapper.SaveChangesAsync() > 0) | ||
| { | ||
| return Result.Ok(_mapper.Map<ProgramDto>(entity)); | ||
| } | ||
|
|
||
| return Result.Fail<ProgramDto>(ProgramConstants.FailedToCreateProgram); | ||
| } | ||
| catch (ValidationException ex) | ||
| { | ||
| return Result.Fail<ProgramDto>(ex.Message); | ||
| } | ||
| } | ||
| } | ||
5 changes: 5 additions & 0 deletions
5
VictoryCenter/VictoryCenter.BLL/Commands/Programs/Delete/DeleteProgramCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| using MediatR; | ||
| using FluentResults; | ||
| namespace VictoryCenter.BLL.Commands.Programs.Delete; | ||
|
|
||
| public record DeleteProgramCommand(long Id) : IRequest<Result<long>>; |
44 changes: 44 additions & 0 deletions
44
VictoryCenter/VictoryCenter.BLL/Commands/Programs/Delete/DeleteProgramHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| using MediatR; | ||
| using FluentResults; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using VictoryCenter.BLL.Constants; | ||
| using VictoryCenter.DAL.Entities; | ||
| using VictoryCenter.DAL.Repositories.Options; | ||
| using VictoryCenter.DAL.Repositories.Interfaces.Base; | ||
|
|
||
| namespace VictoryCenter.BLL.Commands.Programs.Delete; | ||
|
|
||
| public class DeleteProgramHandler : IRequestHandler<DeleteProgramCommand, Result<long>> | ||
| { | ||
| private readonly IRepositoryWrapper _repositoryWrapper; | ||
|
|
||
| public DeleteProgramHandler(IRepositoryWrapper repositoryWrapper) | ||
| { | ||
| _repositoryWrapper = repositoryWrapper; | ||
| } | ||
|
|
||
| public async Task<Result<long>> Handle(DeleteProgramCommand request, CancellationToken cancellationToken) | ||
| { | ||
| var entityToDelete = await _repositoryWrapper.ProgramsRepository.GetFirstOrDefaultAsync(new QueryOptions<Program> | ||
| { | ||
| Filter = program => program.Id == request.Id, | ||
| Include = program => program.Include(p => p.Categories) | ||
| }); | ||
|
|
||
| if (entityToDelete is null) | ||
| { | ||
| return Result.Fail<long>(ErrorMessagesConstants | ||
| .NotFound(request.Id, typeof(Program))); | ||
| } | ||
|
|
||
| entityToDelete.Categories.Clear(); | ||
| _repositoryWrapper.ProgramsRepository.Delete(entityToDelete); | ||
|
Kitukl marked this conversation as resolved.
|
||
|
|
||
| if (await _repositoryWrapper.SaveChangesAsync() > 0) | ||
| { | ||
| return Result.Ok(entityToDelete.Id); | ||
| } | ||
|
|
||
| return Result.Fail(ProgramConstants.FailedToDeleteProgram); | ||
| } | ||
|
Kitukl marked this conversation as resolved.
|
||
| } | ||
7 changes: 7 additions & 0 deletions
7
VictoryCenter/VictoryCenter.BLL/Commands/Programs/Update/UpdateProgramCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| using MediatR; | ||
| using FluentResults; | ||
| using VictoryCenter.BLL.DTOs.Programs; | ||
|
|
||
| namespace VictoryCenter.BLL.Commands.Programs.Update; | ||
|
|
||
| public record UpdateProgramCommand(UpdateProgramDto updateProgramDto) : IRequest<Result<ProgramDto>>; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.