Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oil mark #41

Merged
merged 3 commits into from
Feb 13, 2025
Merged
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
19 changes: 6 additions & 13 deletions CheckDrive.Api/CheckDrive.Api/Controllers/OilMarksController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,28 @@ namespace CheckDrive.Api.Controllers;

[Route("api/oilMarks")]
[ApiController]
public class OilMarksController : ControllerBase
public class OilMarksController(IOilMarkService oilMarkService) : ControllerBase
{
private readonly IOilMarkService _oilMarkService;

public OilMarksController(IOilMarkService oilMarkService)
{
_oilMarkService = oilMarkService ?? throw new ArgumentNullException(nameof(oilMarkService));
}

[HttpGet]
public async Task<ActionResult<List<OilMarkDto>>> GetAsync([FromQuery] OilMarkQueryParameters queryParameters)
{
var oilMarks = await _oilMarkService.GetAllAsync(queryParameters);
var oilMarks = await oilMarkService.GetAllAsync(queryParameters);

return Ok(oilMarks);
}

[HttpGet("{id:int}", Name = "GetOilMarkById")]
public async Task<ActionResult<OilMarkDto>> GetByIdAsync(int id)
{
var oilMark = await _oilMarkService.GetByIdAsync(id);
var oilMark = await oilMarkService.GetByIdAsync(id);

return oilMark;
}

[HttpPost]
public async Task<ActionResult<OilMarkDto>> CreateAsync(CreateOilMarkDto oilMark)
{
var createdOilMark = await _oilMarkService.CreateAsync(oilMark);
var createdOilMark = await oilMarkService.CreateAsync(oilMark);

return CreatedAtAction("GetOilMarkById", oilMark, new { id = createdOilMark.Id });
}
Expand All @@ -48,15 +41,15 @@ public async Task<ActionResult<OilMarkDto>> UpdateAsync(int id, UpdateOilMarkDto
return BadRequest($"Route parameter id: {id} does not match with body parameter id: {oilMark.Id}.");
}

var updatedOilMark = await _oilMarkService.UpdateAsync(oilMark);
var updatedOilMark = await oilMarkService.UpdateAsync(oilMark);

return Ok(updatedOilMark);
}

[HttpDelete("{id:int}")]
public async Task<ActionResult> DeleteAsync(int id)
{
await _oilMarkService.DeleteAsync(id);
await oilMarkService.DeleteAsync(id);

return NoContent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ public sealed class OilMarkMappings : Profile
public OilMarkMappings()
{
CreateMap<OilMark, OilMarkDto>();
CreateMap<CreateOilMarkDto, OilMark>();
CreateMap<CreateOilMarkDto, OilMark>()
.ForPath(dest => dest.Name, opt => opt.MapFrom(src => src.Name));

CreateMap<UpdateOilMarkDto, OilMark>();
}
}
92 changes: 37 additions & 55 deletions CheckDrive.Api/CheckDrive.Application/Services/OilMarkService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,98 +9,80 @@

namespace CheckDrive.Application.Services;

internal sealed class OilMarkService : IOilMarkService
internal sealed class OilMarkService(ICheckDriveDbContext context, IMapper mapper) : IOilMarkService
{
private readonly ICheckDriveDbContext _context;
private readonly IMapper _mapper;

public OilMarkService(ICheckDriveDbContext context, IMapper mapper)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
}

public async Task<OilMarkDto> CreateAsync(CreateOilMarkDto oilMark)
{
ArgumentNullException.ThrowIfNull(oilMark);

var entity = _mapper.Map<OilMark>(oilMark);

_context.OilMarks.Add(entity);
await _context.SaveChangesAsync();

var dto = _mapper.Map<OilMarkDto>(entity);

return dto;
}

public async Task DeleteAsync(int id)
{
var entity = await _context.OilMarks.FirstOrDefaultAsync(x => x.Id == id);

if (entity is null)
{
throw new EntityNotFoundException($"Oil mark with id: {id} is not found.");
}

_context.OilMarks.Remove(entity);
await _context.SaveChangesAsync();
}

public Task<List<OilMarkDto>> GetAllAsync()
{
var oilMarks = _context.OilMarks
.Select(x => new OilMarkDto(x.Id, x.Name))
.ToListAsync();

return oilMarks;
}

public async Task<List<OilMarkDto>> GetAllAsync(OilMarkQueryParameters queryParameters)
{
ArgumentNullException.ThrowIfNull(queryParameters);

var query = _context.OilMarks.AsNoTracking().AsQueryable();
var query = context.OilMarks.AsNoTracking().AsQueryable();

if (!string.IsNullOrEmpty(queryParameters.SearchText))
{
query = query.Where(x => x.Name.Contains(queryParameters.SearchText));
}

var entities = await query.ToListAsync();
var dtos = _mapper.Map<List<OilMarkDto>>(entities);
var dtos = mapper.Map<List<OilMarkDto>>(entities);

return dtos;
}

public async Task<OilMarkDto> GetByIdAsync(int id)
{
var entity = await _context.OilMarks.FirstOrDefaultAsync(x => x.Id == id);
var entity = await context.OilMarks.FirstOrDefaultAsync(x => x.Id == id);

if (entity is null)
{
throw new EntityNotFoundException($"Oil mark with id: {id} is not found.");
}

var dto = _mapper.Map<OilMarkDto>(entity);
var dto = mapper.Map<OilMarkDto>(entity);

return dto;
}

public async Task<OilMarkDto> UpdateAsync(UpdateOilMarkDto oilMark)
{
if (!await _context.OilMarks.AnyAsync(x => x.Id == oilMark.Id))
if (!await context.OilMarks.AnyAsync(x => x.Id == oilMark.Id))
{
throw new EntityNotFoundException($"Oil mark with id: {oilMark.Id} is not found.");
}

var entity = _mapper.Map<OilMark>(oilMark);
var entity = mapper.Map<OilMark>(oilMark);

context.OilMarks.Update(entity);
await context.SaveChangesAsync();

var dto = mapper.Map<OilMarkDto>(entity);

return dto;
}

public async Task<OilMarkDto> CreateAsync(CreateOilMarkDto oilMark)
{
ArgumentNullException.ThrowIfNull(oilMark);

var entity = mapper.Map<OilMark>(oilMark);

_context.OilMarks.Update(entity);
await _context.SaveChangesAsync();
context.OilMarks.Add(entity);
await context.SaveChangesAsync();

var dto = _mapper.Map<OilMarkDto>(entity);
var dto = mapper.Map<OilMarkDto>(entity);

return dto;
}

public async Task DeleteAsync(int id)
{
var entity = await context.OilMarks.FirstOrDefaultAsync(x => x.Id == id);

if (entity is null)
{
throw new EntityNotFoundException($"Oil mark with id: {id} is not found.");
}

context.OilMarks.Remove(entity);
await context.SaveChangesAsync();
}
}
Loading