diff --git a/cat_cafe/Controllers/CustomersController.cs b/cat_cafe/Controllers/CustomersController.cs new file mode 100644 index 0000000..398c9dd --- /dev/null +++ b/cat_cafe/Controllers/CustomersController.cs @@ -0,0 +1,108 @@ +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() + { + return await _context.Customers.ToListAsync(); + } + + // GET: api/Customers/5 + [HttpGet("{id}")] + public async Task> GetCustomer(long id) + { + 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) + { + _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) + { + 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); + } + } +} 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/Customer.cs b/cat_cafe/Entities/Customer.cs new file mode 100644 index 0000000..5ea6d82 --- /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/Mappers/CustomerMapper.cs b/cat_cafe/Mappers/CustomerMapper.cs new file mode 100644 index 0000000..d0f38ea --- /dev/null +++ b/cat_cafe/Mappers/CustomerMapper.cs @@ -0,0 +1,16 @@ +using AutoMapper; +using cat_cafe.Dto; +using cat_cafe.Entities; + + +namespace cat_cafe.Mappers +{ + public class CustomerMapper:Profile + { + public CustomerMapper() + { + CreateMap().ReverseMap(); + } + } +} + diff --git a/cat_cafe/Program.cs b/cat_cafe/Program.cs index dae595a..e5cb26c 100644 --- a/cat_cafe/Program.cs +++ b/cat_cafe/Program.cs @@ -10,6 +10,8 @@ builder.Services.AddDbContext(opt => opt.UseInMemoryDatabase("CatCafe")); 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/Repositories/CustomerContext.cs b/cat_cafe/Repositories/CustomerContext.cs new file mode 100644 index 0000000..a96292f --- /dev/null +++ b/cat_cafe/Repositories/CustomerContext.cs @@ -0,0 +1,14 @@ +using System; +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!; + } +} + diff --git a/cat_cafe/Services/CatsService.cs b/cat_cafe/Services/CatsService.cs deleted file mode 100644 index 8516d8f..0000000 --- a/cat_cafe/Services/CatsService.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace cat_cafe.Services -{ - public class CatsService - { - } -}