From 2a9329f7b7a3d1109d00c99d7acc19aa4eb871b0 Mon Sep 17 00:00:00 2001 From: Evenya Date: Wed, 22 Sep 2021 00:35:32 +0300 Subject: [PATCH 1/4] Query Task added --- .../Queries/GetTheoryBlockByIdQuery.cs | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 server/StudySharp.ApplicationServices/Queries/GetTheoryBlockByIdQuery.cs diff --git a/server/StudySharp.ApplicationServices/Queries/GetTheoryBlockByIdQuery.cs b/server/StudySharp.ApplicationServices/Queries/GetTheoryBlockByIdQuery.cs new file mode 100644 index 0000000..c471503 --- /dev/null +++ b/server/StudySharp.ApplicationServices/Queries/GetTheoryBlockByIdQuery.cs @@ -0,0 +1,38 @@ +using System.Threading; +using System.Threading.Tasks; +using MediatR; +using StudySharp.Domain.Constants; +using StudySharp.Domain.General; +using StudySharp.Domain.Models; +using StudySharp.DomainServices; + +namespace StudySharp.ApplicationServices.Queries +{ + public sealed class GetTheoryBlockByIdQuery : IRequest> + { + public int Id { get; set; } + } + + public sealed class GetTheoryBlockByIdQueryHandler : IRequestHandler> + { + private readonly StudySharpDbContext _context; + + public GetTheoryBlockByIdQueryHandler(StudySharpDbContext studySharpDbContext) + { + _context = studySharpDbContext; + } + + public async Task> Handle( + GetTheoryBlockByIdQuery request, + CancellationToken cancellationToken) + { + var theoryBlock = await _context.TheoryBlocks.FindAsync(request.Id, cancellationToken); + if (theoryBlock == null) + { + return OperationResult.Fail(string.Format(ErrorConstants.EntityNotFound, nameof(TheoryBlock), nameof(TheoryBlock.Id), request.Id)); + } + + return OperationResult.Ok(theoryBlock); + } + } +} \ No newline at end of file From f253469d11bb8c6f12a50eb508b4754a754ee79a Mon Sep 17 00:00:00 2001 From: Evenya Date: Mon, 11 Oct 2021 13:03:56 +0300 Subject: [PATCH 2/4] Add TagController.cs and Tag Requests/Responses --- .../Controllers/TagController.cs | 57 +++++++++++++++++++ .../Requests/Tags/AddTagRequest.cs | 9 +++ .../Requests/Tags/GetTagByIdRequest.cs | 10 ++++ .../Requests/Tags/RemoveTagByIdRequest.cs | 10 ++++ .../Responses/Tags/AddTagResponse.cs | 6 ++ .../Responses/Tags/GetTagByIdResponse.cs | 18 ++++++ .../Responses/Tags/RemoveTagByIdResponse.cs | 6 ++ 7 files changed, 116 insertions(+) create mode 100644 server/StudySharp.API/Controllers/TagController.cs create mode 100644 server/StudySharp.API/Requests/Tags/AddTagRequest.cs create mode 100644 server/StudySharp.API/Requests/Tags/GetTagByIdRequest.cs create mode 100644 server/StudySharp.API/Requests/Tags/RemoveTagByIdRequest.cs create mode 100644 server/StudySharp.API/Responses/Tags/AddTagResponse.cs create mode 100644 server/StudySharp.API/Responses/Tags/GetTagByIdResponse.cs create mode 100644 server/StudySharp.API/Responses/Tags/RemoveTagByIdResponse.cs diff --git a/server/StudySharp.API/Controllers/TagController.cs b/server/StudySharp.API/Controllers/TagController.cs new file mode 100644 index 0000000..2e90d78 --- /dev/null +++ b/server/StudySharp.API/Controllers/TagController.cs @@ -0,0 +1,57 @@ +using System.Threading.Tasks; +using AutoMapper; +using MediatR; +using Microsoft.AspNetCore.Mvc; +using StudySharp.API.Requests.Tags; +using StudySharp.API.Responses.Tags; +using StudySharp.ApplicationServices.Commands; +using StudySharp.ApplicationServices.Queries; +using StudySharp.Domain.General; + +namespace StudySharp.API.Controllers +{ + // [Authorize] + [ApiController] + [Route("api/courses")] + + public class TagController : ControllerBase + { + private readonly IMediator _mediator; + private readonly IMapper _mapper; + + public TagController(IMediator mediator, IMapper mapper) + { + _mediator = mediator; + _mapper = mapper; + } + + [HttpPost] + public async Task Add([FromBody] AddTagRequest addTagRequest) + { + var addTagCommand = _mapper.Map(addTagRequest); + return await _mediator.Send(addTagCommand); + } + + [HttpGet("{id:int}")] + public async Task> GetTagById([FromRoute] GetTagByIdRequest getTagByIdRequest) + { + var getTagByIdQuery = _mapper.Map(getTagByIdRequest); + var operationResult = await _mediator.Send(getTagByIdQuery); + + if (!operationResult.IsSucceeded) + { + return OperationResult.Fail(operationResult.Errors); + } + + var response = _mapper.Map(operationResult.Result); + return OperationResult.Ok(response); + } + + [HttpDelete("{id:int}")] + public async Task Remove([FromBody] RemoveTagByIdRequest removeTagByIdRequest) + { + var removeTagByIdCommand = _mapper.Map(removeTagByIdRequest); + return await _mediator.Send(removeTagByIdCommand); + } + } +} \ No newline at end of file diff --git a/server/StudySharp.API/Requests/Tags/AddTagRequest.cs b/server/StudySharp.API/Requests/Tags/AddTagRequest.cs new file mode 100644 index 0000000..4438c23 --- /dev/null +++ b/server/StudySharp.API/Requests/Tags/AddTagRequest.cs @@ -0,0 +1,9 @@ +namespace StudySharp.API.Requests.Tags +{ + public class AddTagRequest + { + public string Name { get; set; } + public int TeacherId { get; set; } + public int CourseId { get; set; } + } +} \ No newline at end of file diff --git a/server/StudySharp.API/Requests/Tags/GetTagByIdRequest.cs b/server/StudySharp.API/Requests/Tags/GetTagByIdRequest.cs new file mode 100644 index 0000000..fb712d9 --- /dev/null +++ b/server/StudySharp.API/Requests/Tags/GetTagByIdRequest.cs @@ -0,0 +1,10 @@ +using Microsoft.AspNetCore.Mvc; + +namespace StudySharp.API.Requests.Tags +{ + public class GetTagByIdRequest + { + [BindProperty(Name = "id", SupportsGet = true)] + public int Id { get; set; } + } +} \ No newline at end of file diff --git a/server/StudySharp.API/Requests/Tags/RemoveTagByIdRequest.cs b/server/StudySharp.API/Requests/Tags/RemoveTagByIdRequest.cs new file mode 100644 index 0000000..7833bc1 --- /dev/null +++ b/server/StudySharp.API/Requests/Tags/RemoveTagByIdRequest.cs @@ -0,0 +1,10 @@ +using Microsoft.AspNetCore.Mvc; + +namespace StudySharp.API.Requests.Tags +{ + public class RemoveTagByIdRequest + { + [BindProperty(Name = "id", SupportsGet = true)] + public int Id { get; set; } + } +} \ No newline at end of file diff --git a/server/StudySharp.API/Responses/Tags/AddTagResponse.cs b/server/StudySharp.API/Responses/Tags/AddTagResponse.cs new file mode 100644 index 0000000..b1b115c --- /dev/null +++ b/server/StudySharp.API/Responses/Tags/AddTagResponse.cs @@ -0,0 +1,6 @@ +namespace StudySharp.API.Responses.Tags +{ + public class AddTagResponse + { + } +} \ No newline at end of file diff --git a/server/StudySharp.API/Responses/Tags/GetTagByIdResponse.cs b/server/StudySharp.API/Responses/Tags/GetTagByIdResponse.cs new file mode 100644 index 0000000..d217f77 --- /dev/null +++ b/server/StudySharp.API/Responses/Tags/GetTagByIdResponse.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using StudySharp.Domain.Models; + +namespace StudySharp.API.Responses.Tags +{ + public class GetTagByIdResponse + { + public int Id { get; set; } + public string Name { get; set; } + public int TeacherId { get; set; } + public Teacher Teacher { get; set; } + public List TheoryBlocks { get; set; } + public List PracticalBlocks { get; set; } + public DateTimeOffset DateCreated { get; private set; } + public DateTimeOffset? DateModified { get; private set; } + } +} \ No newline at end of file diff --git a/server/StudySharp.API/Responses/Tags/RemoveTagByIdResponse.cs b/server/StudySharp.API/Responses/Tags/RemoveTagByIdResponse.cs new file mode 100644 index 0000000..7ee3885 --- /dev/null +++ b/server/StudySharp.API/Responses/Tags/RemoveTagByIdResponse.cs @@ -0,0 +1,6 @@ +namespace StudySharp.API.Responses.Tags +{ + public class RemoveTagByIdResponse + { + } +} \ No newline at end of file From 9bc9409b565ed2c08f54573464ffa7b1b9b1ac37 Mon Sep 17 00:00:00 2001 From: Evenya Date: Mon, 18 Oct 2021 22:25:56 +0300 Subject: [PATCH 3/4] TagController.cs minor fixes --- server/StudySharp.API/Controllers/TagController.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/server/StudySharp.API/Controllers/TagController.cs b/server/StudySharp.API/Controllers/TagController.cs index 2e90d78..9b182ec 100644 --- a/server/StudySharp.API/Controllers/TagController.cs +++ b/server/StudySharp.API/Controllers/TagController.cs @@ -12,14 +12,14 @@ namespace StudySharp.API.Controllers { // [Authorize] [ApiController] - [Route("api/courses")] + [Route("api/tags")] - public class TagController : ControllerBase + public class GetTagsController : ControllerBase { private readonly IMediator _mediator; private readonly IMapper _mapper; - public TagController(IMediator mediator, IMapper mapper) + public GetTagsController(IMediator mediator, IMapper mapper) { _mediator = mediator; _mapper = mapper; From 4db6d1f805c14e42d1b8265f9fae7f2457866209 Mon Sep 17 00:00:00 2001 From: zefirlover Date: Mon, 18 Oct 2021 23:02:43 +0300 Subject: [PATCH 4/4] Add GetTagsQuery (hotfix) --- .../Queries/GetTagsQuery.cs | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 server/StudySharp.ApplicationServices/Queries/GetTagsQuery.cs diff --git a/server/StudySharp.ApplicationServices/Queries/GetTagsQuery.cs b/server/StudySharp.ApplicationServices/Queries/GetTagsQuery.cs new file mode 100644 index 0000000..c3a3553 --- /dev/null +++ b/server/StudySharp.ApplicationServices/Queries/GetTagsQuery.cs @@ -0,0 +1,31 @@ +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using MediatR; +using Microsoft.EntityFrameworkCore; +using StudySharp.Domain.General; +using StudySharp.Domain.Models; +using StudySharp.DomainServices; + +namespace StudySharp.ApplicationServices.Queries +{ + public sealed class GetTagsQuery : IRequest>> + { + } + + public sealed class GetTagsQueryHandler : IRequestHandler>> + { + private readonly StudySharpDbContext _context; + + public GetTagsQueryHandler(StudySharpDbContext studySharpDbContext) + { + _context = studySharpDbContext; + } + + public async Task>> Handle(GetTagsQuery request, CancellationToken cancellationToken) + { + var tags = await _context.Tags.ToListAsync(cancellationToken); + return OperationResult.Ok(tags); + } + } +} \ No newline at end of file