Merge pull request 'Customer merge req' (#42) from customer_branche_TRO into master
continuous-integration/drone/push Build is passing Details

Reviewed-on: #42
pull/51/head^2
Alexis Drai 2 years ago
commit f14c41b921

BIN
.DS_Store vendored

Binary file not shown.

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

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

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

@ -1,8 +1,17 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using cat_cafe.Repositories; using cat_cafe.Repositories;
using Serilog;
using Serilog.Sinks.File;
var builder = WebApplication.CreateBuilder(args); 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. // Add services to the container.
builder.Services.AddControllers(); builder.Services.AddControllers();
@ -21,6 +30,12 @@ builder.Services.AddControllersWithViews();
var app = builder.Build(); 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. // Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
{ {
@ -34,4 +49,5 @@ app.UseAuthorization();
app.MapControllers(); app.MapControllers();
Log.Information("program start");
app.Run(); app.Run();

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

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

Loading…
Cancel
Save