Merge pull request 'improve-cats' (#52) from improve-cats into master
continuous-integration/drone/push Build is passing Details

Reviewed-on: #52
pull/53/head
Alexis Drai 2 years ago
commit f85b32a408

2
.gitignore vendored

@ -2,6 +2,7 @@
# Edit at https://www.toptal.com/developers/gitignore?templates=dotnetcore,visualstudio,visualstudiocode # Edit at https://www.toptal.com/developers/gitignore?templates=dotnetcore,visualstudio,visualstudiocode
log.txt log.txt
cat_cafe/log.txt
### DotnetCore ### ### DotnetCore ###
# .NET Core build folders # .NET Core build folders
@ -427,3 +428,4 @@ FodyWeavers.xsd
# Additional files built by Visual Studio # Additional files built by Visual Studio
# End of https://www.toptal.com/developers/gitignore/api/dotnetcore,visualstudio,visualstudiocode # End of https://www.toptal.com/developers/gitignore/api/dotnetcore,visualstudio,visualstudiocode
/Tests/Tests - Backup.csproj

@ -0,0 +1,127 @@
using AutoMapper;
using Castle.Core.Logging;
using cat_cafe.Controllers;
using cat_cafe.Dto;
using cat_cafe.Entities;
using cat_cafe.Mappers;
using cat_cafe.Repositories;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FluentAssertions;
namespace cat_cafe.Controllers.Tests
{
[TestClass()]
public class CatsControllerTest
{
private readonly ILogger<CatsController> logger = new NullLogger<CatsController>();
private readonly MapperConfiguration mapperConf = new(mapper => mapper.AddProfile(typeof(CatMapper)));
private readonly DbContextOptions<CatCafeContext> options = new DbContextOptionsBuilder<CatCafeContext>()
.UseInMemoryDatabase(databaseName: "CatCafeTests")
.Options;
private readonly IMapper mapper;
private readonly CatCafeContext context;
private readonly CatsController controller;
public CatsControllerTest()
{
mapper = mapperConf.CreateMapper();
context = new CatCafeContext(options);
controller = new CatsController(context, mapper, logger);
}
[TestInitialize]
public void BeforeEach()
{
context.Database.EnsureCreated();
context.Cats.AddRange(
new Cat
{
Id = 1,
Name = "Alice",
Age = 5
},
new Cat
{
Id = 2,
Name = "Bob",
Age = 3
});
context.SaveChanges();
}
[TestCleanup]
public void AfterEach()
{
context.Database.EnsureDeleted();
}
[TestMethod()]
public async Task GetCatsTest()
{
var actual = await controller.GetCats();
actual.Result.Should().BeOfType<OkObjectResult>();
var actualResult = actual.Result as OkObjectResult;
actualResult.Should().NotBeNull();
actualResult!.Value.Should().BeEquivalentTo(new List<CatDto>()
{
new CatDto
{
Id = 1,
Name = "Alice",
},
new CatDto
{
Id = 2,
Name = "Bob",
}
}.AsEnumerable());
}
[TestMethod()]
public void GetCatTest()
{
Assert.IsTrue(true);
}
[TestMethod()]
public void PutCatTest()
{
Assert.IsTrue(true);
}
[TestMethod()]
public void PostCatTest()
{
Assert.IsTrue(true);
}
[TestMethod()]
public void DeleteCatTest()
{
Assert.IsTrue(true);
}
}
}

@ -9,24 +9,16 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="xunit" Version="2.4.1" /> <PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"> <PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
<PrivateAssets>all</PrivateAssets> <PackageReference Include="coverlet.collector" Version="3.1.2" />
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\cat_cafe\cat_cafe.csproj" /> <ProjectReference Include="..\cat_cafe\cat_cafe.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Remove="AutoMapper.Extensions.Microsoft.DependencyInjection" />
</ItemGroup>
</Project> </Project>

@ -1,23 +0,0 @@
using cat_cafe.Entities;
namespace Tests
{
public class UnitTest1
{
[Fact]
public void Test1()
{
String name = "Margot";
int id = 1337;
Cat cat = new()
{
Id = id,
Name = name
};
Assert.Equal(name, cat.Name);
Assert.Equal(id, cat.Id);
}
}
}

@ -1 +1 @@
global using Xunit; global using Microsoft.VisualStudio.TestTools.UnitTesting;

@ -18,11 +18,11 @@ namespace cat_cafe.Controllers
[ApiController] [ApiController]
public class BarsController : ControllerBase public class BarsController : ControllerBase
{ {
private readonly BarContext _context; private readonly CatCafeContext _context;
private readonly IMapper _mapper; private readonly IMapper _mapper;
private readonly ILogger <BarsController> _logger; private readonly ILogger <BarsController> _logger;
public BarsController(BarContext context,IMapper mapper, ILogger<BarsController> logger) public BarsController(CatCafeContext context,IMapper mapper, ILogger<BarsController> logger)
{ {
_context = context; _context = context;
_mapper = mapper; _mapper = mapper;

@ -9,6 +9,9 @@ using cat_cafe.Entities;
using cat_cafe.Repositories; using cat_cafe.Repositories;
using AutoMapper; using AutoMapper;
using cat_cafe.Dto; using cat_cafe.Dto;
using Serilog;
using Newtonsoft.Json;
using Microsoft.Extensions.Logging.Abstractions;
namespace cat_cafe.Controllers namespace cat_cafe.Controllers
{ {
@ -16,13 +19,15 @@ namespace cat_cafe.Controllers
[ApiController] [ApiController]
public class CatsController : ControllerBase public class CatsController : ControllerBase
{ {
private readonly CatContext _context; private readonly CatCafeContext _context;
private readonly IMapper _mapper; private readonly IMapper _mapper;
private readonly ILogger<CatsController> _logger;
public CatsController(CatContext context, IMapper mapper) public CatsController(CatCafeContext context, IMapper mapper, ILogger<CatsController> logger)
{ {
_mapper = mapper; _mapper = mapper;
_context = context; _context = context;
_logger = logger;
} }
// GET: api/Cats // GET: api/Cats
@ -30,8 +35,8 @@ namespace cat_cafe.Controllers
public async Task<ActionResult<IEnumerable<CatDto>>> GetCats() public async Task<ActionResult<IEnumerable<CatDto>>> GetCats()
{ {
var cats = await _context.Cats.ToListAsync(); var cats = await _context.Cats.ToListAsync();
return _mapper.Map<List<CatDto>>(cats);
return Ok(_mapper.Map<List<CatDto>>(cats));
} }
// GET: api/Cats/5 // GET: api/Cats/5
@ -45,7 +50,7 @@ namespace cat_cafe.Controllers
return NotFound(); return NotFound();
} }
return _mapper.Map<CatDto>(cat); return Ok(_mapper.Map<CatDto>(cat));
} }
// PUT: api/Cats/5 // PUT: api/Cats/5

@ -18,11 +18,11 @@ namespace cat_cafe.Controllers
[ApiController] [ApiController]
public class CustomersController : ControllerBase public class CustomersController : ControllerBase
{ {
private readonly CustomerContext _context; private readonly CatCafeContext _context;
private readonly IMapper _mapper; private readonly IMapper _mapper;
private readonly ILogger<CustomersController> _logger; private readonly ILogger<CustomersController> _logger;
public CustomersController(CustomerContext context,IMapper mapper,ILogger<CustomersController> logger) public CustomersController(CatCafeContext context,IMapper mapper,ILogger<CustomersController> logger)
{ {
_context = context; _context = context;
_mapper = mapper; _mapper = mapper;

@ -5,6 +5,5 @@ namespace cat_cafe.Dto
{ {
public long Id { get; set; } public long Id { get; set; }
public string? Name { get; set; } public string? Name { get; set; }
public int Age { get; set; }
} }
} }

@ -1,9 +1,12 @@
namespace cat_cafe.Entities using System.ComponentModel.DataAnnotations;
namespace cat_cafe.Entities
{ {
public class Cat public class Cat
{ {
public long Id { get; set; } public long Id { get; set; }
public string? Name { get; set; } public string? Name { get; set; }
[Required]
public int Age { get; set; } = 0; public int Age { get; set; } = 0;
public string Meow() { return "meow"; } public string Meow() { return "meow"; }

@ -5,37 +5,21 @@ 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(); 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();
builder.Services.AddDbContext<CatContext>(opt => builder.Services.AddDbContext<CatCafeContext>(opt => opt.UseInMemoryDatabase("CatCafe"));
opt.UseInMemoryDatabase("CatCafe"));
builder.Services.AddDbContext<BarContext>(opt =>
opt.UseInMemoryDatabase("CatCafe"));
builder.Services.AddDbContext<CustomerContext>(opt =>
opt.UseInMemoryDatabase("CatCafe"));
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
builder.Services.AddAutoMapper(typeof(Program)); builder.Services.AddAutoMapper(typeof(Program));
builder.Services.AddControllersWithViews(); 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(); app.UseHttpLogging();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
{ {
@ -50,4 +34,5 @@ app.UseAuthorization();
app.MapControllers(); app.MapControllers();
Log.Information("program start"); Log.Information("program start");
app.Run(); app.Run();

@ -1,17 +0,0 @@
using cat_cafe.Entities;
using Microsoft.EntityFrameworkCore;
using System;
namespace cat_cafe.Repositories
{
public class BarContext : DbContext
{
public BarContext(DbContextOptions<BarContext> options)
: base(options)
{
}
public DbSet<Bar> Bars { get; set; } = null!;
}
}

@ -3,14 +3,16 @@ using Microsoft.EntityFrameworkCore;
namespace cat_cafe.Repositories namespace cat_cafe.Repositories
{ {
public class CatContext : DbContext public class CatCafeContext : DbContext
{ {
public CatContext(DbContextOptions<CatContext> options) public CatCafeContext(DbContextOptions<CatCafeContext> options)
: base(options) : base(options)
{ {
} }
public DbSet<Cat> Cats { get; set; } = null!; public DbSet<Cat> Cats { get; set; } = null!;
public DbSet<Bar> Bars { get; set; } = null!;
public DbSet<Customer> Customers { get; set; } = null!;
} }
} }

@ -1,14 +0,0 @@
using System;
using cat_cafe.Entities;
using Microsoft.EntityFrameworkCore;
namespace cat_cafe.Repositories
{
public class CustomerContext:DbContext
{
public CustomerContext(DbContextOptions<CustomerContext> options)
: base(options)
{ }
public DbSet<Customer> Customers { get; set; } = null!;
}
}

@ -5,7 +5,7 @@ VisualStudioVersion = 17.2.32616.157
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "cat_cafe", "cat_cafe.csproj", "{CC02D05A-3817-4D0A-8766-3FEE0C90941B}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "cat_cafe", "cat_cafe.csproj", "{CC02D05A-3817-4D0A-8766-3FEE0C90941B}"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests", "..\Tests\Tests.csproj", "{674DDACC-DB0E-473B-81E2-E3CABB8CB65B}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "..\Tests\Tests.csproj", "{039A9A95-25ED-4632-9C4B-0AB4E5B5A7B4}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -17,10 +17,10 @@ Global
{CC02D05A-3817-4D0A-8766-3FEE0C90941B}.Debug|Any CPU.Build.0 = Debug|Any CPU {CC02D05A-3817-4D0A-8766-3FEE0C90941B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CC02D05A-3817-4D0A-8766-3FEE0C90941B}.Release|Any CPU.ActiveCfg = Release|Any CPU {CC02D05A-3817-4D0A-8766-3FEE0C90941B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CC02D05A-3817-4D0A-8766-3FEE0C90941B}.Release|Any CPU.Build.0 = Release|Any CPU {CC02D05A-3817-4D0A-8766-3FEE0C90941B}.Release|Any CPU.Build.0 = Release|Any CPU
{674DDACC-DB0E-473B-81E2-E3CABB8CB65B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {039A9A95-25ED-4632-9C4B-0AB4E5B5A7B4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{674DDACC-DB0E-473B-81E2-E3CABB8CB65B}.Debug|Any CPU.Build.0 = Debug|Any CPU {039A9A95-25ED-4632-9C4B-0AB4E5B5A7B4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{674DDACC-DB0E-473B-81E2-E3CABB8CB65B}.Release|Any CPU.ActiveCfg = Release|Any CPU {039A9A95-25ED-4632-9C4B-0AB4E5B5A7B4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{674DDACC-DB0E-473B-81E2-E3CABB8CB65B}.Release|Any CPU.Build.0 = Release|Any CPU {039A9A95-25ED-4632-9C4B-0AB4E5B5A7B4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

@ -1,28 +0,0 @@
2023-01-21 09:37:45.148 +01:00 [INF] program start
2023-01-28 08:16:52.320 +01:00 [INF] program start
2023-01-28 08:30:32.772 +01:00 [INF] program start
2023-01-28 08:31:51.147 +01:00 [INF] program start
2023-01-28 08:35:42.533 +01:00 [INF] program start
2023-01-28 08:37:29.403 +01:00 [INF] program start
2023-01-28 08:38:02.513 +01:00 [INF] program start
2023-01-28 08:38:24.151 +01:00 [INF] program start
2023-01-28 08:39:11.319 +01:00 [INF] program start
2023-01-28 08:54:25.005 +01:00 [INF] program start
2023-01-28 08:55:19.776 +01:00 [INF] program start
2023-01-28 08:57:05.150 +01:00 [INF] POST => post customer
2023-01-28 08:57:05.326 +01:00 [INF] POST => 201 cat_cafe.Entities.Customer {"Id":1,"FullName":"string","Age":0}
2023-01-28 08:57:13.908 +01:00 [INF] GET => get All customers
2023-01-28 08:57:13.997 +01:00 [INF] GET => 200 System.Collections.Generic.List`1[cat_cafe.Entities.Customer] length[1]
2023-01-28 08:58:25.856 +01:00 [INF] program start
2023-01-28 09:05:05.071 +01:00 [INF] program start
2023-01-28 09:13:54.542 +01:00 [INF] program start
2023-01-28 09:17:54.058 +01:00 [INF] program start
2023-01-28 09:24:15.797 +01:00 [INF] program start
2023-01-28 09:26:42.943 +01:00 [INF] program start
2023-01-28 09:31:25.523 +01:00 [INF] program start
2023-01-28 09:38:42.245 +01:00 [INF] program start
2023-01-28 09:40:40.846 +01:00 [INF] program start
2023-01-28 09:49:33.194 +01:00 [INF] program start
2023-01-28 09:52:36.098 +01:00 [INF] program start
2023-01-28 09:56:02.340 +01:00 [INF] program start
2023-01-28 09:57:05.155 +01:00 [INF] program start
Loading…
Cancel
Save