-
Notifications
You must be signed in to change notification settings - Fork 2
STD-40 | Realize TheoryBlock Controller #37
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 all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
2a9329f
Query Task added
Evenya d4ccb27
created: // AddTheoryBlockCommand.cs
Evenya bdd649e
Added course controller and course requests and responses
maridodonova 3af6452
Fixed http get/post
maridodonova c926da9
Added course mapper profile
maridodonova 961285b
Fixwd conversions
Evenya d125f65
Argument Fix
Evenya 1319b62
Merge remote-tracking branch 'origin/STD-35' into STD-40
zefirlover 9cd3df0
AddTheoryBlockRequest, RemoveTheoryBlockByIdRequest and TheoryBlockCo…
zefirlover 08aad56
Merge branch 'dev' into STD-40
zefirlover 852e078
Fix Equals and code style
SleepyF0X ef6d376
Merge remote-tracking branch 'origin/STD-35' into STD-40
zefirlover 8ca9cc7
HttpGet by Id added into TheoryBlockController.cs and other minor cha…
zefirlover c718f99
After review update
zefirlover 3163021
Remove stupid generics
zefirlover a5fe2be
GetTheoryBlockByCourseId pre-update
zefirlover 6d3d9fe
Add "Update" functionality to the TheoryBlockController
zefirlover 35725c1
Add GetTheoryBlockByCourseId
zefirlover ee7f7c5
Pre-rewrite update
zefirlover 3ce1e4b
Merge remote-tracking branch 'origin/STD-38' into STD-40
zefirlover ca150d9
Major update of the TheoryBlockController
zefirlover 88da422
GetTheoryBlockByCourseIdQuery returns a List of TheoryBlocks if theor…
zefirlover 9f9af27
Merge branch 'dev' into STD-40
zefirlover 9f0b2a2
Pull update from dev
zefirlover 24fbe12
Add Course validation
zefirlover ea7fa14
Remove useless comment
zefirlover 5888538
Minor changes
zefirlover ee51462
Remove unnecessary Course elements
zefirlover 2b7c5bf
Tests 1/2
zefirlover 3998fcd
Tests 2/2
zefirlover 440fa69
Tests 3/2
zefirlover 74fcf17
Add now works, rewrite operation result text in GetTheoryBlockByIdQuery
zefirlover 7ac7cfd
Rework courseId exception in Remove, GetById, GetByCourseId, rework e…
zefirlover dce2fb4
Unnecessary comments was removed
zefirlover da94b00
Change course to courseExistent
zefirlover 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
91 changes: 91 additions & 0 deletions
91
server/StudySharp.API/Controllers/TheoryBlockController.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,91 @@ | ||
| using System.Threading.Tasks; | ||
| using AutoMapper; | ||
| using MediatR; | ||
| using Microsoft.AspNetCore.Authorization; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using StudySharp.API.Requests.TheoryBlocks; | ||
| using StudySharp.API.Responses.TheoryBlocks; | ||
| using StudySharp.ApplicationServices.Commands; | ||
| using StudySharp.ApplicationServices.Queries; | ||
| using StudySharp.Domain.General; | ||
|
|
||
| namespace StudySharp.API.Controllers | ||
| { | ||
| [Authorize] | ||
| [ApiController] | ||
| [Route("api/courses")] | ||
|
|
||
| public class TheoryBlockController : ControllerBase | ||
| { | ||
| private readonly IMediator _mediator; | ||
| private readonly IMapper _mapper; | ||
|
|
||
| public TheoryBlockController(IMediator mediator, IMapper mapper) | ||
| { | ||
| _mapper = mapper; | ||
| _mediator = mediator; | ||
| } | ||
|
|
||
| [HttpPost("{courseId:int}/theory-blocks")] | ||
| public async Task<OperationResult> Add([FromRoute] int courseId, [FromBody] AddTheoryBlockRequest addTheoryBlockRequest) | ||
| { | ||
| var addTheoryBlockCommand = _mapper.Map<AddTheoryBlockCommand>(addTheoryBlockRequest); | ||
| addTheoryBlockCommand.CourseId = courseId; | ||
| return await _mediator.Send(addTheoryBlockCommand); | ||
| } | ||
|
|
||
| [HttpDelete("{courseId:int}/theory-blocks/{id:int}")] | ||
| public async Task<OperationResult> Remove([FromRoute] RemoveTheoryBlockByIdRequest removeTheoryBlockByIdRequest) | ||
| { | ||
| var removeTheoryBlockByIdCommand = _mapper.Map<RemoveTheoryBlockByIdCommand>(removeTheoryBlockByIdRequest); | ||
| return await _mediator.Send(removeTheoryBlockByIdCommand); | ||
| } | ||
|
|
||
| [HttpGet("{courseId:int}/theory-blocks/{id:int}")] | ||
| public async Task<OperationResult<GetTheoryBlockByIdResponse>> GetTheoryBlockById([FromRoute] GetTheoryBlockByIdRequest getTheoryBlockByIdRequest) | ||
| { | ||
| var getTheoryBlockByIdQuery = _mapper.Map<GetTheoryBlockByIdQuery>(getTheoryBlockByIdRequest); | ||
| var operationResult = await _mediator.Send(getTheoryBlockByIdQuery); | ||
|
|
||
| if (!operationResult.IsSucceeded) | ||
| { | ||
| return OperationResult.Fail<GetTheoryBlockByIdResponse>(operationResult.Errors); | ||
| } | ||
|
|
||
| var response = _mapper.Map<GetTheoryBlockByIdResponse>(operationResult.Result); | ||
| return OperationResult.Ok(response); | ||
| } | ||
|
|
||
| [HttpPut("{courseId:int}/theory-blocks/{id:int}")] | ||
| public async Task<OperationResult<UpdateTheoryBlockResponse>> Update([FromRoute] int id, [FromRoute] int courseId, [FromBody] UpdateTheoryBlockByIdRequest updateTheoryBlockByIdRequest) | ||
| { | ||
| var updateTheoryBlockCommand = _mapper.Map<UpdateTheoryBlockCommand>(updateTheoryBlockByIdRequest); | ||
|
zefirlover marked this conversation as resolved.
|
||
| updateTheoryBlockCommand.Id = id; | ||
| updateTheoryBlockCommand.CourseId = courseId; | ||
| var operationResult = await _mediator.Send(updateTheoryBlockCommand); | ||
|
|
||
| if (!operationResult.IsSucceeded) | ||
| { | ||
| return OperationResult.Fail<UpdateTheoryBlockResponse>(operationResult.Errors); | ||
| } | ||
|
|
||
| var response = _mapper.Map<UpdateTheoryBlockResponse>(operationResult); | ||
| return OperationResult.Ok(response); | ||
| } | ||
|
|
||
| [HttpGet("{courseId:int}/theory-blocks")] | ||
| public async Task<OperationResult<GetTheoryBlocksByCourseIdResponse>> GetTheoryBlocksByCourseId([FromRoute] GetTheoryBlocksByCourseIdRequest getTheoryBlockByCourseIdRequest) | ||
| { | ||
| var getTheoryBlockByCourseIdQuery = _mapper.Map<GetTheoryBlocksByCourseIdQuery>(getTheoryBlockByCourseIdRequest); | ||
| var operationResult = await _mediator.Send(getTheoryBlockByCourseIdQuery); | ||
|
|
||
| if (!operationResult.IsSucceeded) | ||
| { | ||
| return OperationResult.Fail<GetTheoryBlocksByCourseIdResponse>(operationResult.Errors); | ||
| } | ||
|
|
||
| var response = _mapper.Map<GetTheoryBlocksByCourseIdResponse>(operationResult.Result); | ||
| return OperationResult.Ok(response); | ||
| } | ||
| } | ||
| } | ||
19 changes: 19 additions & 0 deletions
19
server/StudySharp.API/MapperProfiles/TheoryBlockProfile.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,19 @@ | ||
| using AutoMapper; | ||
| using StudySharp.API.Requests.TheoryBlocks; | ||
| using StudySharp.ApplicationServices.Commands; | ||
| using StudySharp.ApplicationServices.Queries; | ||
|
|
||
| namespace StudySharp.API.MapperProfiles | ||
| { | ||
| public class TheoryBlockProfile : Profile | ||
| { | ||
| public TheoryBlockProfile() | ||
| { | ||
| CreateMap<AddTheoryBlockRequest, AddTheoryBlockCommand>(); | ||
| CreateMap<RemoveTheoryBlockByIdRequest, RemoveTheoryBlockByIdCommand>(); | ||
| CreateMap<GetTheoryBlockByIdRequest, GetTheoryBlockByIdQuery>(); | ||
| CreateMap<UpdateTheoryBlockByIdRequest, UpdateTheoryBlockCommand>(); | ||
| CreateMap<GetTheoryBlocksByCourseIdRequest, GetTheoryBlocksByCourseIdQuery>(); | ||
| } | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
server/StudySharp.API/Requests/TheoryBlocks/AddTheoryBlockRequest.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,10 @@ | ||
| using Microsoft.AspNetCore.Mvc; | ||
|
|
||
| namespace StudySharp.API.Requests.TheoryBlocks | ||
| { | ||
| public class AddTheoryBlockRequest | ||
| { | ||
| public string Name { get; set; } | ||
| public string Description { get; set; } | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
server/StudySharp.API/Requests/TheoryBlocks/GetTheoryBlockByIdRequest.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,12 @@ | ||
| using Microsoft.AspNetCore.Mvc; | ||
|
|
||
| namespace StudySharp.API.Requests.TheoryBlocks | ||
| { | ||
| public class GetTheoryBlockByIdRequest | ||
| { | ||
| [BindProperty(Name = "id", SupportsGet = true)] | ||
| public int Id { get; set; } | ||
|
zefirlover marked this conversation as resolved.
|
||
| [BindProperty(Name = "courseId", SupportsGet = true)] | ||
| public int CourseId { get; set; } | ||
| } | ||
| } | ||
10 changes: 10 additions & 0 deletions
10
server/StudySharp.API/Requests/TheoryBlocks/GetTheoryBlocksByCourseIdRequest.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,10 @@ | ||
| using Microsoft.AspNetCore.Mvc; | ||
|
|
||
| namespace StudySharp.API.Requests.TheoryBlocks | ||
| { | ||
| public class GetTheoryBlocksByCourseIdRequest | ||
| { | ||
| [BindProperty(Name = "courseId", SupportsGet = true)] | ||
| public int CourseId { get; set; } | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
server/StudySharp.API/Requests/TheoryBlocks/RemoveTheoryBlockByIdRequest.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,12 @@ | ||
| using Microsoft.AspNetCore.Mvc; | ||
|
|
||
| namespace StudySharp.API.Requests.TheoryBlocks | ||
| { | ||
| public class RemoveTheoryBlockByIdRequest | ||
|
zefirlover marked this conversation as resolved.
|
||
| { | ||
| [BindProperty(Name = "id", SupportsGet = true)] | ||
|
zefirlover marked this conversation as resolved.
|
||
| public int Id { get; set; } | ||
|
zefirlover marked this conversation as resolved.
|
||
| [BindProperty(Name = "courseId", SupportsGet = true)] | ||
| public int CourseId { get; set; } | ||
| } | ||
| } | ||
8 changes: 8 additions & 0 deletions
8
server/StudySharp.API/Requests/TheoryBlocks/UpdateTheoryBlockByIdRequest.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 @@ | ||
| namespace StudySharp.API.Requests.TheoryBlocks | ||
| { | ||
| public class UpdateTheoryBlockByIdRequest | ||
|
zefirlover marked this conversation as resolved.
zefirlover marked this conversation as resolved.
|
||
| { | ||
| public string Name { get; set; } | ||
| public string Description { get; set; } | ||
| } | ||
| } | ||
13 changes: 13 additions & 0 deletions
13
server/StudySharp.API/Responses/TheoryBlocks/GetTheoryBlockByIdResponse.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,13 @@ | ||
| using StudySharp.Domain.Models; | ||
|
|
||
| namespace StudySharp.API.Responses.TheoryBlocks | ||
| { | ||
| public class GetTheoryBlockByIdResponse | ||
| { | ||
| public int Id { get; set; } | ||
| public string Name { get; set; } | ||
| public string Description { get; set; } | ||
| public int CourseId { get; set; } | ||
| public Course Course { get; set; } | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
server/StudySharp.API/Responses/TheoryBlocks/GetTheoryBlocksByCourseIdResponse.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,10 @@ | ||
| using System.Collections.Generic; | ||
| using StudySharp.Domain.Models; | ||
|
|
||
| namespace StudySharp.API.Responses.TheoryBlocks | ||
| { | ||
| public class GetTheoryBlocksByCourseIdResponse | ||
| { | ||
| public List<TheoryBlock> TheoryBlocks { get; set; } | ||
| } | ||
| } |
6 changes: 6 additions & 0 deletions
6
server/StudySharp.API/Responses/TheoryBlocks/UpdateTheoryBlockResponse.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 @@ | ||
| namespace StudySharp.API.Responses.TheoryBlocks | ||
| { | ||
| public class UpdateTheoryBlockResponse | ||
| { | ||
| } | ||
| } |
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
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
51 changes: 51 additions & 0 deletions
51
server/StudySharp.ApplicationServices/Commands/UpdateTheoryBlockCommand.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 System.Threading; | ||
| using System.Threading.Tasks; | ||
| using MediatR; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using StudySharp.Domain.Constants; | ||
| using StudySharp.Domain.General; | ||
| using StudySharp.Domain.Models; | ||
| using StudySharp.DomainServices; | ||
|
|
||
| namespace StudySharp.ApplicationServices.Commands | ||
| { | ||
| public sealed class UpdateTheoryBlockCommand : IRequest<OperationResult> | ||
|
zefirlover marked this conversation as resolved.
|
||
| { | ||
| public int Id { get; set; } | ||
| public int CourseId { get; set; } | ||
| public string Name { get; set; } | ||
| public string Description { get; set; } | ||
| } | ||
|
|
||
| public sealed class UpdateTheoryBlockCommandHandler : IRequestHandler<UpdateTheoryBlockCommand, OperationResult> | ||
| { | ||
| private readonly StudySharpDbContext _context; | ||
|
|
||
| public UpdateTheoryBlockCommandHandler(StudySharpDbContext sharpDbContext) | ||
| { | ||
| _context = sharpDbContext; | ||
| } | ||
|
|
||
| public async Task<OperationResult> Handle(UpdateTheoryBlockCommand request, CancellationToken cancellationToken) | ||
| { | ||
|
zefirlover marked this conversation as resolved.
|
||
| var courseExistent = await _context.Courses.AnyAsync(_ => _.Id == request.CourseId, cancellationToken); | ||
| if (!courseExistent) | ||
| { | ||
| return OperationResult.Fail(string.Format(ErrorConstants.EntityNotFound, nameof(Course), nameof(Course.Id), request.CourseId)); | ||
| } | ||
|
|
||
| var theoryBlock = await _context.TheoryBlocks.FirstOrDefaultAsync(_ => _.Id == request.Id, cancellationToken); | ||
| if (theoryBlock == null) | ||
| { | ||
| return OperationResult.Fail(string.Format(ErrorConstants.EntityNotFound, nameof(TheoryBlock), nameof(TheoryBlock.Id), request.Id)); | ||
| } | ||
|
|
||
| theoryBlock.Name = request.Name; | ||
| theoryBlock.Description = request.Description; | ||
|
|
||
| _context.TheoryBlocks.Update(theoryBlock); | ||
| await _context.SaveChangesAsync(cancellationToken); | ||
| return OperationResult.Ok(); | ||
| } | ||
| } | ||
| } | ||
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
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.