Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions server/StudySharp.API/Controllers/TagController.cs
Original file line number Diff line number Diff line change
@@ -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
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetTagsController => TagController

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Я имел в виду, что метод, который должен возвращать все теги, отсутствует

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Его нужно добавить

{
private readonly IMediator _mediator;
private readonly IMapper _mapper;

public GetTagsController(IMediator mediator, IMapper mapper)
{
_mediator = mediator;
_mapper = mapper;
}

[HttpPost]
public async Task<OperationResult> Add([FromBody] AddTagRequest addTagRequest)
{
var addTagCommand = _mapper.Map<AddTagCommand>(addTagRequest);
return await _mediator.Send(addTagCommand);
}

[HttpGet("{id:int}")]
public async Task<OperationResult<GetTagByIdResponse>> GetTagById([FromRoute] GetTagByIdRequest getTagByIdRequest)
{
var getTagByIdQuery = _mapper.Map<GetTagByIdQuery>(getTagByIdRequest);
var operationResult = await _mediator.Send(getTagByIdQuery);

if (!operationResult.IsSucceeded)
{
return OperationResult.Fail<GetTagByIdResponse>(operationResult.Errors);
}

var response = _mapper.Map<GetTagByIdResponse>(operationResult.Result);
return OperationResult.Ok(response);
}

[HttpDelete("{id:int}")]
public async Task<OperationResult> Remove([FromBody] RemoveTagByIdRequest removeTagByIdRequest)
{
var removeTagByIdCommand = _mapper.Map<RemoveTagByIdCommand>(removeTagByIdRequest);
return await _mediator.Send(removeTagByIdCommand);
}
}
}
9 changes: 9 additions & 0 deletions server/StudySharp.API/Requests/Tags/AddTagRequest.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
10 changes: 10 additions & 0 deletions server/StudySharp.API/Requests/Tags/GetTagByIdRequest.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
10 changes: 10 additions & 0 deletions server/StudySharp.API/Requests/Tags/RemoveTagByIdRequest.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
6 changes: 6 additions & 0 deletions server/StudySharp.API/Responses/Tags/AddTagResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace StudySharp.API.Responses.Tags
{
public class AddTagResponse
{
}
}
18 changes: 18 additions & 0 deletions server/StudySharp.API/Responses/Tags/GetTagByIdResponse.cs
Original file line number Diff line number Diff line change
@@ -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<TheoryBlock> TheoryBlocks { get; set; }
public List<PracticalBlock> PracticalBlocks { get; set; }
public DateTimeOffset DateCreated { get; private set; }
public DateTimeOffset? DateModified { get; private set; }
}
}
6 changes: 6 additions & 0 deletions server/StudySharp.API/Responses/Tags/RemoveTagByIdResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace StudySharp.API.Responses.Tags
{
public class RemoveTagByIdResponse
{
}
}
31 changes: 31 additions & 0 deletions server/StudySharp.ApplicationServices/Queries/GetTagsQuery.cs
Original file line number Diff line number Diff line change
@@ -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<OperationResult<List<Tag>>>
{
}

public sealed class GetTagsQueryHandler : IRequestHandler<GetTagsQuery, OperationResult<List<Tag>>>
{
private readonly StudySharpDbContext _context;

public GetTagsQueryHandler(StudySharpDbContext studySharpDbContext)
{
_context = studySharpDbContext;
}

public async Task<OperationResult<List<Tag>>> Handle(GetTagsQuery request, CancellationToken cancellationToken)
{
var tags = await _context.Tags.ToListAsync(cancellationToken);
return OperationResult.Ok(tags);
}
}
}