Skip to content

Commit

Permalink
Krasnova/workshop application hot fix (#256)
Browse files Browse the repository at this point in the history
* fixed Application mapping

* changed Application update to work with tracked entities

* fixed Update method in ApplicationRepository

* fixed tests for ApplicationController

* updated range validation for ShortApplicationModel

* removed unnecessary usings
  • Loading branch information
P0linux authored Jul 30, 2021
1 parent c03b299 commit fc0feb5
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using OutOfSchool.Services.Models;
Expand Down Expand Up @@ -37,5 +35,25 @@ public async Task<IEnumerable<Application>> Create(IEnumerable<Application> appl
await db.SaveChangesAsync();
return await Task.FromResult(applications);
}

/// <summary>
/// Update information about element.
/// </summary>
/// <param name="entity">Entity to update.</param>
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.
/// The task result contains the entity that was updated.</returns>
/// <exception cref="DbUpdateException">An exception that is thrown when an error is encountered while saving to the database.</exception>
/// <exception cref="DbUpdateConcurrencyException">If a concurrency violation is encountered while saving to database.</exception>
public new async Task<Application> Update(Application entity)
{
var application = dbSet.Find(entity.Id);

db.Entry(application).CurrentValues.SetValues(entity);

db.Entry(application).State = EntityState.Modified;

await this.db.SaveChangesAsync();
return entity;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ public async Task UpdateApplication_WhenModelIsValid_ShouldReturnOkObjectResult(
providerService.Setup(s => s.GetByUserId(userId)).ReturnsAsync(provider);

applicationService.Setup(s => s.Update(applications.First())).ReturnsAsync(applications.First());
applicationService.Setup(s => s.GetByIdNoTracking(shortApplication.Id)).ReturnsAsync(applications.First());
applicationService.Setup(s => s.GetById(shortApplication.Id)).ReturnsAsync(applications.First());

// Act
var result = await controller.Update(shortApplication).ConfigureAwait(false) as OkObjectResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ public async Task<IActionResult> Update(ShortApplicationDto applicationDto)
return BadRequest(ModelState);
}

var application = await applicationService.GetByIdNoTracking(applicationDto.Id).ConfigureAwait(false);
var application = await applicationService.GetById(applicationDto.Id).ConfigureAwait(false);

if (application is null)
{
Expand Down
25 changes: 8 additions & 17 deletions OutOfSchool/OutOfSchool.WebApi/Extensions/MappingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,15 @@ public static AddressDto ToModel(this Address address)
return Mapper<Address, AddressDto>(address, cfg => { cfg.CreateMap<Address, AddressDto>(); });
}

public static ApplicationDto ToShortModel(this Application application)
{
return Mapper<Application, ApplicationDto>(application, cfg =>
{
cfg.CreateMap<Workshop, WorkshopDTO>()
.ForMember(w => w.Address, m => m.Ignore())
.ForMember(w => w.Teachers, m => m.Ignore());
cfg.CreateMap<Child, ChildDto>()
.ForMember(c => c.BirthCertificate, m => m.Ignore())
.ForMember(c => c.Parent, m => m.Ignore());
cfg.CreateMap<Parent, ParentDTO>();
cfg.CreateMap<Application, ApplicationDto>();
});
}

public static ApplicationDto ToModel(this Application application)
{
return Mapper<Application, ApplicationDto>(application, cfg =>
{
cfg.CreateMap<Address, AddressDto>();
cfg.CreateMap<Teacher, TeacherDTO>();
cfg.CreateMap<Workshop, WorkshopDTO>()
.ForMember(dest => dest.Direction, opt => opt.MapFrom(src => src.Direction.Title));
.ForMember(dest => dest.Direction, opt => opt.MapFrom(src => src.Direction.Title))
.ForMember(dest => dest.Keywords, opt => opt.MapFrom(src => src.Keywords.Split('¤', StringSplitOptions.None)));
cfg.CreateMap<BirthCertificate, BirthCertificateDto>();
cfg.CreateMap<Child, ChildDto>()
.ForMember(c => c.Parent, m => m.Ignore());
Expand Down Expand Up @@ -188,7 +174,12 @@ public static Application ToDomain(this ApplicationDto applicationDTO)
{
return Mapper<ApplicationDto, Application>(applicationDTO, cfg =>
{
cfg.CreateMap<WorkshopDTO, Workshop>();
cfg.CreateMap<AddressDto, Address>();
cfg.CreateMap<TeacherDTO, Teacher>();
cfg.CreateMap<WorkshopDTO, Workshop>()
.ForMember(dest => dest.Keywords, opt => opt.MapFrom(src => string.Join('¤', src.Keywords.Distinct())))
.ForMember(dest => dest.Direction, opt => opt.Ignore());
cfg.CreateMap<BirthCertificateDto, BirthCertificate>();
cfg.CreateMap<ChildDto, Child>();
cfg.CreateMap<ParentDTO, Parent>();
cfg.CreateMap<ApplicationDto, Application>();
Expand Down
5 changes: 1 addition & 4 deletions OutOfSchool/OutOfSchool.WebApi/Models/ShortApplicationDTO.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using OutOfSchool.Services.Enums;

namespace OutOfSchool.WebApi.Models
Expand All @@ -12,7 +9,7 @@ public class ShortApplicationDto
public long Id { get; set; }

[Required]
[Range(0, 2, ErrorMessage = "Status should be from 0 to 2")]
[Range(1, 5, ErrorMessage = "Status should be from 1 to 5")]
public ApplicationStatus Status { get; set; } = ApplicationStatus.Pending;
}
}
11 changes: 0 additions & 11 deletions OutOfSchool/OutOfSchool.WebApi/Services/ApplicationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,6 @@ public async Task<ApplicationDto> GetById(long id)
return application.ToModel();
}

/// <inheritdoc/>
public async Task<ApplicationDto> GetByIdNoTracking(long id)
{
Expression<Func<Application, bool>> filter = a => a.Id == id;

var application = await applicationRepository.GetByFilterNoTracking(filter, "Workshop,Child,Parent")
.FirstOrDefaultAsync().ConfigureAwait(false);

return application?.ToShortModel();
}

public async Task<ApplicationDto> Update(ApplicationDto applicationDto)
{
logger.Information($"Updating Application with Id = {applicationDto?.Id} started.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ public interface IApplicationService
/// <returns>Application.</returns>
Task<ApplicationDto> GetById(long id);

/// <summary>
/// Get entity by it's key as no tracking.
/// </summary>
/// <param name="id">Key in the table.</param>
/// <returns>Application.</returns>
Task<ApplicationDto> GetByIdNoTracking(long id);

/// <summary>
/// Get applications by workshop id.
/// </summary>
Expand Down

0 comments on commit fc0feb5

Please sign in to comment.