/
h0tnanny
/
DeliveryService
Обзор
Документация
Войти
/
h0tnanny
/
DeliveryService
Код
Запросы
0
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
DeliveryService.API/Controllers/ClientController.cs
89 строк
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 ClientController(ILogger<ClientController> logger, DeliveryDbContext context) : ControllerBase { [HttpGet("{id:guid}")] [ProducesResponseType<Client>(200)] public async Task<ActionResult<Client>> GetClient(Guid id) { var client = await context.Clients.FirstOrDefaultAsync(c => c.Id == id); if (client is null) { return NotFound(); } return Ok(client); } [HttpGet("GetAll")] [ProducesResponseType<IEnumerable<Client>>(200)] public async Task<IActionResult> GetAllClients() { var clients = await context.Clients.ToListAsync(); return Ok(clients); } [HttpPost("Create")] [ProducesResponseType<Client>(200)] public async Task<IActionResult> CreateClient([FromBody] ClientModel newClient) { var clientCheck = await context.Clients.FirstOrDefaultAsync(c => c.Email == newClient.Email && c.Phone == newClient.Phone); if (clientCheck is not null) return Conflict("Клиент с таким email или телефоном уже существует"); var client = new Client { Id = Guid.NewGuid(), Name = newClient.Name, FirstName = newClient.FirstName, Email = newClient.Email, Phone = newClient.Phone, }; await context.Clients.AddAsync(client); await context.SaveChangesAsync(); return Ok(client); } [HttpPut("Update/{id:guid}")] [ProducesResponseType<Client>(200)] public async Task<IActionResult> UpdateClient(Guid id, [FromBody] ClientModel newClient) { var client = await context.Clients.FirstOrDefaultAsync(c => c.Id == id); if (client is null) return NotFound(); client.Name = newClient.Name; client.FirstName = newClient.FirstName; client.Email = newClient.Email; client.Phone = newClient.Phone; context.Clients.Update(client); await context.SaveChangesAsync(); return Ok(newClient); } [HttpDelete("Delete/{id:guid}")] [ProducesResponseType(204)] public async Task<IActionResult> DeleteClient(Guid id) { var clientToDelete = await context.Clients.FirstOrDefaultAsync(c => c.Id == id); if (clientToDelete is null) return NoContent(); context.Clients.Remove(clientToDelete); await context.SaveChangesAsync(); return NoContent(); } }