/
h0tnanny
/
DeliveryService
Обзор
Документация
Войти
/
h0tnanny
/
DeliveryService
Код
Запросы
0
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
DeliveryService.API/Controllers/CourierController.cs
87 строк
3 KB
Хатнянский Максим
add roles
15 мар 2024, 15:02
15 мар 2024, 15:02
0ebb845
Код
Авторство
О чём код?
using DeliveryService.API.Models; using DeliveryService.Persistence; using DeliveryService.Persistence.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; namespace DeliveryService.API.Controllers; [ApiController] [Route("[controller]")] public sealed class CourierController(ILogger<ClientController> logger, DeliveryDbContext context) : ControllerBase { [HttpGet("{id:guid}")] [ProducesResponseType<Courier>(StatusCodes.Status200OK)] public IActionResult Get(Guid id) { var courier = context.Couriers.FirstOrDefault(x => x.Id == id); if (courier == null) { return NotFound(); } return Ok(courier); } [HttpGet("GetAll")] [ProducesResponseType<IEnumerable<Courier>>(StatusCodes.Status200OK)] public async Task<IActionResult> GetAll() { var couriers = await context.Couriers.ToListAsync(); return Ok(couriers); } [HttpPost("Create")] [ProducesResponseType<Courier>(StatusCodes.Status200OK)] public async Task<IActionResult> Create(CourierModel courier) { var newCourier = new Courier() { Id = Guid.NewGuid(), Name = courier.Name, FirstName = courier.FirstName, Phone = courier.Phone, Email = string.Empty, }; if (context.Couriers.FirstOrDefault(x => x.Id == newCourier.Id) == null) { return BadRequest("Такой курьер уже существует"); } await context.Couriers.AddAsync(newCourier); await context.SaveChangesAsync(); return Ok(newCourier); } [HttpPut("Update/{id:guid}")] [ProducesResponseType<Courier>(StatusCodes.Status200OK)] public async Task<IActionResult> Update(Guid id, [FromBody] CourierModel courier) { var courierToUpdate = context.Couriers.FirstOrDefault(x => x.Id == id); if (courierToUpdate == null) return NotFound(); courierToUpdate.Name = courier.Name; courierToUpdate.FirstName = courier.FirstName; courierToUpdate.Phone = courier.Phone; context.Couriers.Update(courierToUpdate); await context.SaveChangesAsync(); return Ok(courierToUpdate); } [HttpDelete("Delete/{id:guid}")] public async Task<IActionResult> Delete(Guid id) { var courier = context.Couriers.FirstOrDefault(x => x.Id == id); if (courier == null) return NoContent(); context.Couriers.Remove(courier); await context.SaveChangesAsync(); return NoContent(); } }