diff --git a/server/StudySharp.API/Controllers/TagController.cs b/server/StudySharp.API/Controllers/TagController.cs new file mode 100644 index 0000000..9b182ec --- /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/tags")] + + public class GetTagsController : ControllerBase + { + private readonly IMediator _mediator; + private readonly IMapper _mapper; + + public GetTagsController(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 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