From 332f7026857c1f686b69e98b860557d14f1866bc Mon Sep 17 00:00:00 2001 From: "ismail.taha_janan" Date: Tue, 10 Jan 2023 11:08:02 +0100 Subject: [PATCH 1/4] :memo: controller adde for customer --- cat_cafe/Controllers/CustomersController.cs | 124 ++++++++++++++++++++ cat_cafe/Entities/Customer.cs | 11 ++ cat_cafe/Program.cs | 2 + cat_cafe/Properties/launchSettings.json | 5 +- cat_cafe/Repositories/CustomerContext.cs | 14 +++ 5 files changed, 153 insertions(+), 3 deletions(-) create mode 100644 cat_cafe/Controllers/CustomersController.cs create mode 100644 cat_cafe/Entities/Customer.cs create mode 100644 cat_cafe/Repositories/CustomerContext.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/Entities/Customer.cs b/cat_cafe/Entities/Customer.cs new file mode 100644 index 0000000..f4e763f --- /dev/null +++ b/cat_cafe/Entities/Customer.cs @@ -0,0 +1,11 @@ +using System; +namespace cat_cafe.Entities +{ + public class Customer + { + public long Id { get; set; } + public string? FullName { get; set; } + public int Age { get; set; } = 0; + } +} + diff --git a/cat_cafe/Program.cs b/cat_cafe/Program.cs index e1c623e..16a7b2c 100644 --- a/cat_cafe/Program.cs +++ b/cat_cafe/Program.cs @@ -8,6 +8,8 @@ var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(); builder.Services.AddDbContext(opt => opt.UseInMemoryDatabase("CatCafe")); +builder.Services.AddDbContext(opt => +opt.UseInMemoryDatabase("CatCafe")); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); diff --git a/cat_cafe/Properties/launchSettings.json b/cat_cafe/Properties/launchSettings.json index fd816e1..0e99e9a 100644 --- a/cat_cafe/Properties/launchSettings.json +++ b/cat_cafe/Properties/launchSettings.json @@ -1,4 +1,4 @@ -{ +{ "$schema": "https://json.schemastore.org/launchsettings.json", "iisSettings": { "windowsAuthentication": false, @@ -11,7 +11,6 @@ "profiles": { "cat_cafe": { "commandName": "Project", - "dotnetRunMessages": true, "launchBrowser": true, "launchUrl": "swagger", "applicationUrl": "https://localhost:7229;http://localhost:5229", @@ -28,4 +27,4 @@ } } } -} +} \ No newline at end of file diff --git a/cat_cafe/Repositories/CustomerContext.cs b/cat_cafe/Repositories/CustomerContext.cs new file mode 100644 index 0000000..bf1894c --- /dev/null +++ b/cat_cafe/Repositories/CustomerContext.cs @@ -0,0 +1,14 @@ +using cat_cafe.Entities; +using Microsoft.EntityFrameworkCore; + +namespace cat_cafe.Repositories +{ + public class CustomerContext:DbContext + { + public CustomerContext(DbContextOptions options) + :base(options) + {} + public DbSet Customers { get; set; } = null!; + } +} + -- 2.36.3 From a219699271f1230ddf3abd151986cd50e4549262 Mon Sep 17 00:00:00 2001 From: "ismail.taha_janan" Date: Tue, 10 Jan 2023 15:46:00 +0100 Subject: [PATCH 2/4] :beers: i was trying to implement AutoMapper but i was to drunk SORRYgit status --- cat_cafe/Controllers/CustomersController.cs | 5 ++-- cat_cafe/Dto/CatDto.cs | 11 ++++++++ cat_cafe/Dto/CustomerDto.cs | 11 ++++++++ cat_cafe/Entities/Cat.cs | 1 - cat_cafe/Mappers/CustomerMapper.cs | 17 +++++++++++ cat_cafe/Services/CustomeService.cs | 31 +++++++++++++++++++++ cat_cafe/cat_cafe.csproj | 14 ++++++++++ 7 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 cat_cafe/Dto/CatDto.cs create mode 100644 cat_cafe/Dto/CustomerDto.cs create mode 100644 cat_cafe/Mappers/CustomerMapper.cs create mode 100644 cat_cafe/Services/CustomeService.cs diff --git a/cat_cafe/Controllers/CustomersController.cs b/cat_cafe/Controllers/CustomersController.cs index 41ae9e4..881bfa2 100644 --- a/cat_cafe/Controllers/CustomersController.cs +++ b/cat_cafe/Controllers/CustomersController.cs @@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using cat_cafe.Entities; using cat_cafe.Repositories; +using cat_cafe.Dto; namespace cat_cafe.Controllers { @@ -23,7 +24,7 @@ namespace cat_cafe.Controllers // GET: api/Customers [HttpGet] - public async Task>> GetCustomers() + public async Task>> GetCustomers() { if (_context.Customers == null) { @@ -34,7 +35,7 @@ namespace cat_cafe.Controllers // GET: api/Customers/5 [HttpGet("{id}")] - public async Task> GetCustomer(long id) + public async Task> GetCustomer(long id) { if (_context.Customers == null) { diff --git a/cat_cafe/Dto/CatDto.cs b/cat_cafe/Dto/CatDto.cs new file mode 100644 index 0000000..9fd3c53 --- /dev/null +++ b/cat_cafe/Dto/CatDto.cs @@ -0,0 +1,11 @@ +using System; +namespace cat_cafe.Dto +{ + public class CatDto + { + public long Id { get; set; } + public string? Name { get; set; } + public int Age { get; set; } = 0; + } +} + diff --git a/cat_cafe/Dto/CustomerDto.cs b/cat_cafe/Dto/CustomerDto.cs new file mode 100644 index 0000000..6a38d6f --- /dev/null +++ b/cat_cafe/Dto/CustomerDto.cs @@ -0,0 +1,11 @@ +using System; +namespace cat_cafe.Dto +{ + public class CustomerDto + { + public long Id { get; set; } + public string? FullName { get; set; } + public int Age { get; set; } = 0; + } +} + diff --git a/cat_cafe/Entities/Cat.cs b/cat_cafe/Entities/Cat.cs index 0068971..60ff99c 100644 --- a/cat_cafe/Entities/Cat.cs +++ b/cat_cafe/Entities/Cat.cs @@ -6,6 +6,5 @@ public string? Name { get; set; } public int Age { get; set; } = 0; - } } diff --git a/cat_cafe/Mappers/CustomerMapper.cs b/cat_cafe/Mappers/CustomerMapper.cs new file mode 100644 index 0000000..f203515 --- /dev/null +++ b/cat_cafe/Mappers/CustomerMapper.cs @@ -0,0 +1,17 @@ +using System; +using AutoMapper; +using cat_cafe.Entities; +using cat_cafe.Dto; +namespace cat_cafe.Mappers +{ + public class CustomerMapper:Profile + { + + public CustomerMapper() + { + CreateMap(); + CreateMap(); + } + } +} + diff --git a/cat_cafe/Services/CustomeService.cs b/cat_cafe/Services/CustomeService.cs new file mode 100644 index 0000000..ff17ea8 --- /dev/null +++ b/cat_cafe/Services/CustomeService.cs @@ -0,0 +1,31 @@ +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 d01b369..08a7776 100644 --- a/cat_cafe/cat_cafe.csproj +++ b/cat_cafe/cat_cafe.csproj @@ -16,6 +16,20 @@ + + + + + + + + + + + + + + -- 2.36.3 From 682b8a1908ae3dae6dc5e9da86f059a34a0ad559 Mon Sep 17 00:00:00 2001 From: "ismail.taha_janan" Date: Sat, 14 Jan 2023 09:56:41 +0100 Subject: [PATCH 3/4] controller working --- cat_cafe/Controllers/CustomersController.cs | 125 -------------------- cat_cafe/Mappers/CustomerMapper.cs | 17 --- 2 files changed, 142 deletions(-) delete mode 100644 cat_cafe/Controllers/CustomersController.cs delete mode 100644 cat_cafe/Mappers/CustomerMapper.cs diff --git a/cat_cafe/Controllers/CustomersController.cs b/cat_cafe/Controllers/CustomersController.cs deleted file mode 100644 index 881bfa2..0000000 --- a/cat_cafe/Controllers/CustomersController.cs +++ /dev/null @@ -1,125 +0,0 @@ -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; -using cat_cafe.Dto; - -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/Mappers/CustomerMapper.cs b/cat_cafe/Mappers/CustomerMapper.cs deleted file mode 100644 index f203515..0000000 --- a/cat_cafe/Mappers/CustomerMapper.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using AutoMapper; -using cat_cafe.Entities; -using cat_cafe.Dto; -namespace cat_cafe.Mappers -{ - public class CustomerMapper:Profile - { - - public CustomerMapper() - { - CreateMap(); - CreateMap(); - } - } -} - -- 2.36.3 From f974656eccee8d1398a8f94e141b165c62ad1a90 Mon Sep 17 00:00:00 2001 From: "ismail.taha_janan" Date: Sat, 14 Jan 2023 09:57:02 +0100 Subject: [PATCH 4/4] 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 @@ - - - - - - - - -- 2.36.3