diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..4278576 Binary files /dev/null and b/.DS_Store differ diff --git a/cat_cafe/Controllers/CustomersController.cs b/cat_cafe/Controllers/CustomersController.cs index bc62718..e2a5b21 100644 --- a/cat_cafe/Controllers/CustomersController.cs +++ b/cat_cafe/Controllers/CustomersController.cs @@ -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 _logger; - public CustomersController(CustomerContext context,IMapper mapper) + public CustomersController(CustomerContext context,IMapper mapper,ILogger logger) { _context = context; _mapper = mapper; + _logger = logger; } // GET: api/Customers [HttpGet] public async Task>> 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>(customers)); } @@ -37,14 +48,20 @@ namespace cat_cafe.Controllers [HttpGet("{id}")] public async Task> 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(customer); + Log.Information(this.Request.Method + " => " + + this.Response.StatusCode.ToString() + " " + + customer.GetType().ToString() + " " + + JsonConvert.SerializeObject(customer).ToString()); + return Ok(_mapper.Map(customer)); } // PUT: api/Customers/5 @@ -52,8 +69,10 @@ namespace cat_cafe.Controllers [HttpPut("{id}")] public async Task 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> PostCustomer(CustomerDto customerDto) { + Log.Information(this.Request.Method + " => post customer"); + Customer customer = _mapper.Map(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)); } @@ -96,16 +127,24 @@ namespace cat_cafe.Controllers [HttpDelete("{id}")] public async Task 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) diff --git a/cat_cafe/Dto/CustomerDto.cs b/cat_cafe/Dto/CustomerDto.cs index 6a38d6f..181224b 100644 --- a/cat_cafe/Dto/CustomerDto.cs +++ b/cat_cafe/Dto/CustomerDto.cs @@ -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; } } diff --git a/cat_cafe/Entities/Customer.cs b/cat_cafe/Entities/Customer.cs index 5ea6d82..3c264ab 100644 --- a/cat_cafe/Entities/Customer.cs +++ b/cat_cafe/Entities/Customer.cs @@ -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; } } diff --git a/cat_cafe/Program.cs b/cat_cafe/Program.cs index e5cb26c..307d218 100644 --- a/cat_cafe/Program.cs +++ b/cat_cafe/Program.cs @@ -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(); +loggerFactory.AddFile(builder.Configuration["Logging:LogFilePath"].ToString()); + +app.Services.GetService().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(); diff --git a/cat_cafe/appsettings.json b/cat_cafe/appsettings.json index 10f68b8..99488b4 100644 --- a/cat_cafe/appsettings.json +++ b/cat_cafe/appsettings.json @@ -1,9 +1,10 @@ { "Logging": { "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" + "Default": "Information" + //"Microsoft.AspNetCore": "Warning" } }, + "LogFilePath": "Logs\\log-{Date}.txt", "AllowedHosts": "*" } diff --git a/cat_cafe/cat_cafe.csproj b/cat_cafe/cat_cafe.csproj index 38e73c5..31cfb39 100644 --- a/cat_cafe/cat_cafe.csproj +++ b/cat_cafe/cat_cafe.csproj @@ -17,9 +17,15 @@ + + + + + +