From f974656eccee8d1398a8f94e141b165c62ad1a90 Mon Sep 17 00:00:00 2001 From: "ismail.taha_janan" Date: Sat, 14 Jan 2023 09:57:02 +0100 Subject: [PATCH] controller working --- cat_cafe/Controllers/CustomersController.cs | 124 ++++++++++++++++++++ cat_cafe/Services/CustomeService.cs | 31 ----- cat_cafe/cat_cafe.csproj | 8 -- 3 files changed, 124 insertions(+), 39 deletions(-) create mode 100644 cat_cafe/Controllers/CustomersController.cs delete mode 100644 cat_cafe/Services/CustomeService.cs diff --git a/cat_cafe/Controllers/CustomersController.cs b/cat_cafe/Controllers/CustomersController.cs new file mode 100644 index 0000000..41ae9e4 --- /dev/null +++ b/cat_cafe/Controllers/CustomersController.cs @@ -0,0 +1,124 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using cat_cafe.Entities; +using cat_cafe.Repositories; + +namespace cat_cafe.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class CustomersController : ControllerBase + { + private readonly CustomerContext _context; + + public CustomersController(CustomerContext context) + { + _context = context; + } + + // GET: api/Customers + [HttpGet] + public async Task>> GetCustomers() + { + if (_context.Customers == null) + { + return NotFound(); + } + return await _context.Customers.ToListAsync(); + } + + // GET: api/Customers/5 + [HttpGet("{id}")] + public async Task> GetCustomer(long id) + { + if (_context.Customers == null) + { + return NotFound(); + } + var customer = await _context.Customers.FindAsync(id); + + if (customer == null) + { + return NotFound(); + } + + return customer; + } + + // PUT: api/Customers/5 + // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 + [HttpPut("{id}")] + public async Task PutCustomer(long id, Customer customer) + { + if (id != customer.Id) + { + return BadRequest(); + } + + _context.Entry(customer).State = EntityState.Modified; + + try + { + await _context.SaveChangesAsync(); + } + catch (DbUpdateConcurrencyException) + { + if (!CustomerExists(id)) + { + return NotFound(); + } + else + { + throw; + } + } + + return NoContent(); + } + + // POST: api/Customers + // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 + [HttpPost] + public async Task> PostCustomer(Customer customer) + { + if (_context.Customers == null) + { + return Problem("Entity set 'CustomerContext.Customers' is null."); + } + _context.Customers.Add(customer); + await _context.SaveChangesAsync(); + + return CreatedAtAction("GetCustomer", new { id = customer.Id }, customer); + } + + // DELETE: api/Customers/5 + [HttpDelete("{id}")] + public async Task DeleteCustomer(long id) + { + if (_context.Customers == null) + { + return NotFound(); + } + var customer = await _context.Customers.FindAsync(id); + if (customer == null) + { + return NotFound(); + } + + _context.Customers.Remove(customer); + await _context.SaveChangesAsync(); + + return NoContent(); + } + + private bool CustomerExists(long id) + { + return (_context.Customers?.Any(e => e.Id == id)).GetValueOrDefault(); + } + } +} diff --git a/cat_cafe/Services/CustomeService.cs b/cat_cafe/Services/CustomeService.cs deleted file mode 100644 index ff17ea8..0000000 --- a/cat_cafe/Services/CustomeService.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; -using cat_cafe.Entities; -using cat_cafe.Repositories; -using cat_cafe.Dto; -using cat_cafe.Mappers; - -namespace cat_cafe.Services -{ - public class CustomeService - { - public CustomeService() - { - } - - private readonly CustomerContext _context; - - - public Task>? getAll() - { - if (_context.Customers == null) - { - return null; - } - /** - * here i stoped - */ - return Mappers.CustomerMapper(await _context.Customers.ToListAsync()); - } - } -} - diff --git a/cat_cafe/cat_cafe.csproj b/cat_cafe/cat_cafe.csproj index 08a7776..88e9311 100644 --- a/cat_cafe/cat_cafe.csproj +++ b/cat_cafe/cat_cafe.csproj @@ -16,20 +16,12 @@ - - - - - - - -