/
h0tnanny
/
DeliveryService
Обзор
Документация
Войти
/
h0tnanny
/
DeliveryService
Код
Запросы
0
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
DeliveryService.API/Controllers/DiscountController.cs
81 строка
3 KB
Хатнянский Максим
Init
06 мар 2024, 02:20
06 мар 2024, 02:20
e7c6763
Код
Авторство
О чём код?
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 DiscountController(ILogger<DiscountController> logger, DeliveryDbContext context) : ControllerBase { [HttpGet("{id:guid}")] [ProducesResponseType<Discount>(StatusCodes.Status200OK)] public async Task<IActionResult> Get(Guid id) { var discount = await context.Discounts.FirstOrDefaultAsync(x => x.Id == id); if (discount == null) return NotFound(); return Ok(discount); } [HttpGet("GetAll")] [ProducesResponseType<IEnumerable<Discount>>(StatusCodes.Status200OK)] public async Task<IActionResult> GetAll() { var discounts = await context.Discounts.ToListAsync(); return Ok(discounts); } [HttpPost("Create")] [ProducesResponseType<Discount>(StatusCodes.Status200OK)] public async Task<IActionResult> Create(DiscountModel discountModel) { var discount = new Discount { Id = Guid.NewGuid(), Name = discountModel.Name, StartDate = discountModel.StartDate, EndDate = discountModel.EndDate, PercentDiscount = discountModel.PercentDiscount, }; if(await context.Discounts.FirstOrDefaultAsync((x => x.Id == discount.Id)) != null) return BadRequest("Такая скидка уже существует"); context.Discounts.Add(discount); await context.SaveChangesAsync(); return Ok(discount); } [HttpPut("Update/{id:guid}")] [ProducesResponseType<Discount>(StatusCodes.Status200OK)] public async Task<IActionResult> Update(Guid id, DiscountModel discountModel) { var discount = await context.Discounts.FirstOrDefaultAsync(x => x.Id == id); if (discount == null) return NotFound(); discount.Name = discountModel.Name; discount.StartDate = discountModel.StartDate; discount.EndDate = discountModel.EndDate; discount.PercentDiscount = discountModel.PercentDiscount; await context.SaveChangesAsync(); return Ok(discount); } [HttpDelete("Delete/{id:guid}")] [ProducesResponseType(StatusCodes.Status204NoContent)] public async Task<IActionResult> Delete(Guid id) { var discount = await context.Discounts.FirstOrDefaultAsync(x => x.Id == id); if (discount == null) return NotFound(); context.Discounts.Remove(discount); await context.SaveChangesAsync(); return Ok(discount); } }