WIP: customer_branche #22
Closed
ismail.taha_janan
wants to merge 4 commits from customer_branche
into master
@ -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<ActionResult<IEnumerable<Customer>>> GetCustomers()
|
||||||
|
{
|
||||||
|
if (_context.Customers == null)
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
return await _context.Customers.ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET: api/Customers/5
|
||||||
|
[HttpGet("{id}")]
|
||||||
|
public async Task<ActionResult<Customer>> 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<IActionResult> 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<ActionResult<Customer>> 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<IActionResult> 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -6,6 +6,5 @@
|
|||||||||||||
public string? Name { get; set; }
|
public string? Name { get; set; }
|
||||||||||||
public int Age { get; set; } = 0;
|
public int Age { get; set; } = 0;
|
||||||||||||
|
|
||||||||||||
|
|
||||||||||||
alexis.drai
commented 2 years ago
Review
No need to commit that, please use the current version of |
|||||||||||||
}
|
}
|
||||||||||||
}
|
}
|
||||||||||||
|
@ -8,6 +8,8 @@ var builder = WebApplication.CreateBuilder(args);
|
|||||||||||||
builder.Services.AddControllers();
|
builder.Services.AddControllers();
|
||||||||||||
builder.Services.AddDbContext<CatContext>(opt =>
|
builder.Services.AddDbContext<CatContext>(opt =>
|
||||||||||||
opt.UseInMemoryDatabase("CatCafe"));
|
opt.UseInMemoryDatabase("CatCafe"));
|
||||||||||||
|
builder.Services.AddDbContext<CustomerContext>(opt =>
|
||||||||||||
|
opt.UseInMemoryDatabase("CatCafe"));
|
||||||||||||
alexis.drai
commented 2 years ago
Review
Also add
to resolve the conflict |
|||||||||||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||||||||||
builder.Services.AddEndpointsApiExplorer();
|
builder.Services.AddEndpointsApiExplorer();
|
||||||||||||
builder.Services.AddSwaggerGen();
|
builder.Services.AddSwaggerGen();
|
||||||||||||
|
@ -18,4 +18,10 @@
|
|||||||||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
|
||||||||||||
</ItemGroup>
|
</ItemGroup>
|
||||||||||||
|
|
||||||||||||
|
<ItemGroup>
|
||||||||||||
alexis.drai
commented 2 years ago
Review
Is this necessary? I think you can delete lines 21 to 26 (included). |
|||||||||||||
|
<None Remove="Mappers\" />
|
||||||||||||
|
</ItemGroup>
|
||||||||||||
|
<ItemGroup>
|
||||||||||||
|
<Folder Include="Mappers\" />
|
||||||||||||
|
</ItemGroup>
|
||||||||||||
</Project>
|
</Project>
|
||||||||||||
|
Loading…
Reference in new issue
No need to use this Dto, please use the current version of
CatDto
(frommaster
)