Customer merge req #42

Merged
alexis.drai merged 4 commits from customer_branche_TRO into master 2 years ago

BIN
.DS_Store vendored

Binary file not shown.

@ -9,6 +9,8 @@ using cat_cafe.Entities;
using cat_cafe.Repositories;
using cat_cafe.Dto;
using AutoMapper;
using Serilog;
using Newtonsoft.Json;
namespace cat_cafe.Controllers
{
@ -18,18 +20,27 @@ namespace cat_cafe.Controllers
{
private readonly CustomerContext _context;
private readonly IMapper _mapper;
private readonly ILogger<CustomersController> _logger;
public CustomersController(CustomerContext context,IMapper mapper)
public CustomersController(CustomerContext context,IMapper mapper,ILogger<CustomersController> logger)
{
_context = context;
_mapper = mapper;
_logger = logger;
}
// GET: api/Customers
[HttpGet]
public async Task<ActionResult<IEnumerable<CustomerDto>>> GetCustomers()
{
Log.Information(this.Request.Method + " => get All customers");
var customers = await _context.Customers.ToListAsync();
Log.Information(this.Request.Method + " => "
+ this.Response.StatusCode.ToString() + " "
+ customers.GetType().ToString() + " length["
+ customers.Count + "]");
return Ok(_mapper.Map<List<CustomerDto>>(customers));
}
@ -37,14 +48,20 @@ namespace cat_cafe.Controllers
[HttpGet("{id}")]
public async Task<ActionResult<CustomerDto>> GetCustomer(long id)
{
Log.Information(this.Request.Method + " => get by ID {@id}",id);
var customer = await _context.Customers.FindAsync(id);
if (customer == null)
{
Log.Information(this.Request.Method + " => " + NotFound().StatusCode.ToString());
return NotFound();
}
return _mapper.Map<CustomerDto>(customer);
Log.Information(this.Request.Method + " => "
+ this.Response.StatusCode.ToString() + " "
+ customer.GetType().ToString() + " "
+ JsonConvert.SerializeObject(customer).ToString());
return Ok(_mapper.Map<CustomerDto>(customer));
}
// PUT: api/Customers/5
@ -52,8 +69,10 @@ namespace cat_cafe.Controllers
[HttpPut("{id}")]
public async Task<IActionResult> PutCustomer(long id, CustomerDto customerDto)
{
Log.Information(this.Request.Method + " => put by ID {@id}", id);
if (id != customerDto.Id)
{
Log.Information(this.Request.Method + " => " + BadRequest().StatusCode.ToString()+" IDs not matching");
return BadRequest();
}
@ -65,19 +84,24 @@ namespace cat_cafe.Controllers
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
catch (DbUpdateConcurrencyException e)
{
if (!CustomerExists(id))
{
Log.Information(this.Request.Method + " => " + NotFound().StatusCode.ToString());
return NotFound();
}
else
{
Log.Error(this.Request.Method + " => " + e.Message);
throw;
}
}
return NoContent();
Log.Information(this.Request.Method + " => "
+ this.Response.StatusCode.ToString() + " "
+ customer.GetType().ToString() + " "
+ JsonConvert.SerializeObject(customer).ToString());
return Ok();
}
// POST: api/Customers
@ -85,10 +109,17 @@ namespace cat_cafe.Controllers
[HttpPost]
public async Task<ActionResult<Customer>> PostCustomer(CustomerDto customerDto)
{
Log.Information(this.Request.Method + " => post customer");
Customer customer = _mapper.Map<Customer>(customerDto);
_context.Customers.Add(customer);
await _context.SaveChangesAsync();
Log.Information(this.Request.Method + " => "
+ 201 + " "
+ customer.GetType().ToString() + " "
+ JsonConvert.SerializeObject(customer).ToString());
return CreatedAtAction("GetCustomer", new { id = customer.Id }, _mapper.Map<Customer>( customer));
}
@ -96,16 +127,24 @@ namespace cat_cafe.Controllers
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteCustomer(long id)
{
Log.Information(this.Request.Method + " => delete by ID {@id}", id);
var customer = await _context.Customers.FindAsync(id);
if (customer == null)
{
Log.Information(this.Request.Method + " => " + NotFound().StatusCode.ToString());
return NotFound();
}
_context.Customers.Remove(customer);
await _context.SaveChangesAsync();
return NoContent();
Log.Information(this.Request.Method + " => "
+ this.Response.StatusCode.ToString() + " "
+ customer.GetType().ToString() + " "
+ JsonConvert.SerializeObject(customer).ToString());
return Ok();
}
private bool CustomerExists(long id)

@ -1,10 +1,14 @@
using System;
using System.ComponentModel.DataAnnotations;
namespace cat_cafe.Dto
{
public class CustomerDto
{
public long Id { get; set; }
public string? FullName { get; set; }
[Required]
public string FullName { get; set; }
[Required]
public int Age { get; set; } = 0;
}
}

@ -1,10 +1,15 @@
using System;
using System.ComponentModel.DataAnnotations;
namespace cat_cafe.Entities
{
public class Customer
{
public long Id { get; set; }
[Required]
public string? FullName { get; set; }
[Required]
public int Age { get; set; } = 0;
}
}

@ -1,8 +1,17 @@
using Microsoft.EntityFrameworkCore;
using cat_cafe.Repositories;
using Serilog;
using Serilog.Sinks.File;
var builder = WebApplication.CreateBuilder(args);
/*
ILoggerFactory loggerFactory=new LoggerFactory();
loggerFactory.AddFile($@"{Directory.GetCurrentDirectory}\Logs\logs.txt");
*/
Log.Logger = new LoggerConfiguration().MinimumLevel.Information().WriteTo.File("log.txt").CreateLogger();
// Add services to the container.
builder.Services.AddControllers();
@ -21,6 +30,12 @@ builder.Services.AddControllersWithViews();
var app = builder.Build();
/*var loggerFactory = app.Services.GetService<ILoggerFactory>();
loggerFactory.AddFile(builder.Configuration["Logging:LogFilePath"].ToString());
app.Services.GetService<ILoggerFactory>().AddFile(builder.Configuration["Logging:LogFilePath"].ToString());*/
app.UseHttpLogging();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
@ -34,4 +49,5 @@ app.UseAuthorization();
app.MapControllers();
Log.Information("program start");
app.Run();

@ -1,9 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
"Default": "Information"
//"Microsoft.AspNetCore": "Warning"
}
},
"LogFilePath": "Logs\\log-{Date}.txt",
"AllowedHosts": "*"
}

@ -17,9 +17,15 @@
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.11" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />
<PackageReference Include="Serilog.Extensions.Logging.File" Version="3.0.0" />
<PackageReference Include="Serilog" Version="2.12.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
<None Remove="AutoMapper.Extensions.Microsoft.DependencyInjection" />
<None Remove="Serilog.Extensions.Logging.File" />
<None Remove="Serilog" />
<None Remove="Serilog.Sinks.File" />
</ItemGroup>
</Project>

Loading…
Cancel
Save