Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
2a9329f
Query Task added
Evenya Sep 21, 2021
d4ccb27
created: // AddTheoryBlockCommand.cs
Evenya Sep 23, 2021
bdd649e
Added course controller and course requests and responses
maridodonova Sep 24, 2021
3af6452
Fixed http get/post
maridodonova Sep 24, 2021
c926da9
Added course mapper profile
maridodonova Sep 24, 2021
961285b
Fixwd conversions
Evenya Sep 24, 2021
d125f65
Argument Fix
Evenya Sep 24, 2021
1319b62
Merge remote-tracking branch 'origin/STD-35' into STD-40
zefirlover Sep 25, 2021
9cd3df0
AddTheoryBlockRequest, RemoveTheoryBlockByIdRequest and TheoryBlockCo…
zefirlover Sep 25, 2021
08aad56
Merge branch 'dev' into STD-40
zefirlover Sep 25, 2021
852e078
Fix Equals and code style
SleepyF0X Sep 25, 2021
ef6d376
Merge remote-tracking branch 'origin/STD-35' into STD-40
zefirlover Sep 25, 2021
8ca9cc7
HttpGet by Id added into TheoryBlockController.cs and other minor cha…
zefirlover Sep 25, 2021
c718f99
After review update
zefirlover Sep 26, 2021
3163021
Remove stupid generics
zefirlover Sep 26, 2021
a5fe2be
GetTheoryBlockByCourseId pre-update
zefirlover Sep 27, 2021
6d3d9fe
Add "Update" functionality to the TheoryBlockController
zefirlover Sep 28, 2021
35725c1
Add GetTheoryBlockByCourseId
zefirlover Oct 1, 2021
ee7f7c5
Pre-rewrite update
zefirlover Oct 1, 2021
3ce1e4b
Merge remote-tracking branch 'origin/STD-38' into STD-40
zefirlover Oct 1, 2021
ca150d9
Major update of the TheoryBlockController
zefirlover Oct 1, 2021
88da422
GetTheoryBlockByCourseIdQuery returns a List of TheoryBlocks if theor…
zefirlover Oct 1, 2021
9f9af27
Merge branch 'dev' into STD-40
zefirlover Oct 1, 2021
9f0b2a2
Pull update from dev
zefirlover Oct 1, 2021
24fbe12
Add Course validation
zefirlover Oct 2, 2021
ea7fa14
Remove useless comment
zefirlover Oct 2, 2021
5888538
Minor changes
zefirlover Oct 3, 2021
ee51462
Remove unnecessary Course elements
zefirlover Oct 3, 2021
2b7c5bf
Tests 1/2
zefirlover Oct 3, 2021
3998fcd
Tests 2/2
zefirlover Oct 4, 2021
440fa69
Tests 3/2
zefirlover Oct 4, 2021
74fcf17
Add now works, rewrite operation result text in GetTheoryBlockByIdQuery
zefirlover Oct 4, 2021
7ac7cfd
Rework courseId exception in Remove, GetById, GetByCourseId, rework e…
zefirlover Oct 4, 2021
dce2fb4
Unnecessary comments was removed
zefirlover Oct 4, 2021
da94b00
Change course to courseExistent
zefirlover Oct 4, 2021
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
91 changes: 91 additions & 0 deletions server/StudySharp.API/Controllers/TheoryBlockController.cs
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
Comment thread
zefirlover marked this conversation as resolved.
{
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);
Comment thread
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 server/StudySharp.API/MapperProfiles/TheoryBlockProfile.cs
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>();
}
}
}
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; }
}
}
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; }
Comment thread
zefirlover marked this conversation as resolved.
[BindProperty(Name = "courseId", SupportsGet = true)]
public int CourseId { get; set; }
}
}
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; }
}
}
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
Comment thread
zefirlover marked this conversation as resolved.
{
[BindProperty(Name = "id", SupportsGet = true)]
Comment thread
zefirlover marked this conversation as resolved.
public int Id { get; set; }
Comment thread
zefirlover marked this conversation as resolved.
[BindProperty(Name = "courseId", SupportsGet = true)]
public int CourseId { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace StudySharp.API.Requests.TheoryBlocks
{
public class UpdateTheoryBlockByIdRequest
Comment thread
zefirlover marked this conversation as resolved.
Comment thread
zefirlover marked this conversation as resolved.
{
public string Name { get; set; }
public string Description { get; set; }
}
}
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; }
}
}
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; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace StudySharp.API.Responses.TheoryBlocks
{
public class UpdateTheoryBlockResponse
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ public AddTheoryBlockCommandHandler(StudySharpDbContext sharpDbContext)

public async Task<OperationResult> Handle(AddTheoryBlockCommand request, CancellationToken cancellationToken)
Comment thread
zefirlover marked this conversation as resolved.
{
if (await _context.TheoryBlocks.AnyAsync(
_ => _.Name.ToLower().Equals(request.Name.ToLower()) && _.CourseId == request.CourseId,
cancellationToken))
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));
}

if (await _context.TheoryBlocks.AnyAsync(_ => _.Name.ToLower().Equals(request.Name.ToLower()), cancellationToken))
{
return OperationResult.Fail(string.Format(ErrorConstants.EntityAlreadyExists, nameof(TheoryBlock), nameof(TheoryBlock.Name), request.Name));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using Microsoft.EntityFrameworkCore;
using StudySharp.Domain.Constants;
using StudySharp.Domain.General;
using StudySharp.Domain.Models;
Expand All @@ -11,6 +12,7 @@ namespace StudySharp.ApplicationServices.Commands
public sealed class RemoveTheoryBlockByIdCommand : IRequest<OperationResult>
Comment thread
zefirlover marked this conversation as resolved.
{
public int Id { get; set; }
public int CourseId { get; set; }
}

public sealed class RemoveTheoryBlockByIdCommandHandler : IRequestHandler<RemoveTheoryBlockByIdCommand, OperationResult>
Expand All @@ -22,15 +24,19 @@ public RemoveTheoryBlockByIdCommandHandler(StudySharpDbContext sharpDbContext)
_context = sharpDbContext;
}

public async Task<OperationResult> Handle(
RemoveTheoryBlockByIdCommand request,
CancellationToken cancellationToken)
public async Task<OperationResult> Handle(RemoveTheoryBlockByIdCommand request, CancellationToken cancellationToken)
{
Comment thread
zefirlover marked this conversation as resolved.
var theoryBlock = await _context.TheoryBlocks.FindAsync(request.Id, cancellationToken);
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.Name), request.Id));
return OperationResult.Fail(string.Format(ErrorConstants.EntityNotFound, nameof(TheoryBlock), nameof(TheoryBlock.Id), request.Id));
}

_context.TheoryBlocks.Remove(theoryBlock);
Expand Down
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>
Comment thread
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)
{
Comment thread
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();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using Microsoft.EntityFrameworkCore;
using StudySharp.Domain.Constants;
using StudySharp.Domain.General;
using StudySharp.Domain.Models;
Expand All @@ -11,6 +12,7 @@ namespace StudySharp.ApplicationServices.Queries
public sealed class GetTheoryBlockByIdQuery : IRequest<OperationResult<TheoryBlock>>
Comment thread
zefirlover marked this conversation as resolved.
{
public int Id { get; set; }
public int CourseId { get; set; }
}

public sealed class GetTheoryBlockByIdQueryHandler : IRequestHandler<GetTheoryBlockByIdQuery, OperationResult<TheoryBlock>>
Expand All @@ -22,11 +24,15 @@ public GetTheoryBlockByIdQueryHandler(StudySharpDbContext studySharpDbContext)
_context = studySharpDbContext;
}

public async Task<OperationResult<TheoryBlock>> Handle(
GetTheoryBlockByIdQuery request,
CancellationToken cancellationToken)
public async Task<OperationResult<TheoryBlock>> Handle(GetTheoryBlockByIdQuery request, CancellationToken cancellationToken)
{
Comment thread
zefirlover marked this conversation as resolved.
var theoryBlock = await _context.TheoryBlocks.FindAsync(request.Id, cancellationToken);
var courseExistent = await _context.Courses.AnyAsync(_ => _.Id == request.CourseId, cancellationToken);
if (!courseExistent)
{
return OperationResult.Fail<TheoryBlock>(string.Format(ErrorConstants.EntityNotFound, nameof(Course), nameof(Course.Id), request.CourseId));
}

var theoryBlock = await _context.TheoryBlocks.FirstOrDefaultAsync(_ => _.Id == request.Id && _.CourseId == request.CourseId, cancellationToken);
if (theoryBlock == null)
{
return OperationResult.Fail<TheoryBlock>(string.Format(ErrorConstants.EntityNotFound, nameof(TheoryBlock), nameof(TheoryBlock.Id), request.Id));
Expand Down
Loading