customer_branche_TRO
Ismail TAHA JANAN 2 years ago
commit 7b18e512ac

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;

@ -7,6 +7,10 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using cat_cafe.Entities; using cat_cafe.Entities;
using cat_cafe.Repositories; using cat_cafe.Repositories;
using AutoMapper;
using cat_cafe.Dto;
using System.Collections;
using System.Xml.Linq;
namespace cat_cafe.Controllers namespace cat_cafe.Controllers
{ {
@ -15,43 +19,65 @@ namespace cat_cafe.Controllers
public class BarsController : ControllerBase public class BarsController : ControllerBase
{ {
private readonly CatCafeDbContext _context; private readonly CatCafeDbContext _context;
private readonly IMapper _mapper;
private readonly ILogger <BarsController> _logger;
public BarsController(CatCafeDbContext context) public BarsController(CatCafeDbContext context,IMapper mapper, ILogger<BarsController> logger)
{ {
_context = context; _context = context;
_mapper = mapper;
_logger = logger;
} }
// GET: api/Bars // GET: api/Bars
[HttpGet] [HttpGet]
public async Task<ActionResult<IEnumerable<Bar>>> GetBars() public async Task<ActionResult<IEnumerable<BarDto>>> GetBars()
{ {
return await _context.bars.ToListAsync(); //return await _context.bars.ToListAsync();
var bars = _context.bars
.Include(a => a.cats)
.Select(a => new Bar
{
Id = a.Id,
Name = a.Name,
cats = a.cats.Select(p => new Cat { Name = p.Name, Age = p.Age, Id= p.Id}).ToList()
})
.ToListAsync();
return _mapper.Map<List<BarDto>>(bars);
} }
// GET: api/Bars/5 // GET: api/Bars/5
[HttpGet("{id}")] [HttpGet("{id}")]
public async Task<ActionResult<Bar>> GetBar(long id) public async Task<ActionResult<BarDto>> GetBar(long id)
{ {
var bar = await _context.bars.FindAsync(id); //var bar = await _context.bars.FindAsync(id);
var bar = _context.bars.Include(p => p.cats)
.Select(a => new Bar
{
Id = a.Id,
Name = a.Name,
cats = a.cats.Select(p => new Cat { Name = p.Name, Age = p.Age, Id = p.Id }).ToList()
}).FirstOrDefaultAsync(p => p.Id == id);
if (bar == null) if (bar == null)
{ {
return NotFound(); return NotFound();
} }
return bar; return _mapper.Map<BarDto>(bar.Result);
} }
// PUT: api/Bars/5 // PUT: api/Bars/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut("{id}")] [HttpPut("{id}")]
public async Task<IActionResult> PutBar(long id, Bar bar) public async Task<IActionResult> PutBar(long id, BarDto barDto)
{ {
if (id != bar.Id) if (id != barDto.Id)
{ {
return BadRequest(); return BadRequest();
} }
Bar bar = _mapper.Map<Bar>(barDto);
_context.Entry(bar).State = EntityState.Modified; _context.Entry(bar).State = EntityState.Modified;
try try
@ -76,12 +102,12 @@ namespace cat_cafe.Controllers
// POST: api/Bars // POST: api/Bars
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost] [HttpPost]
public async Task<ActionResult<Bar>> PostBar(Bar bar) public async Task<ActionResult<BarDto>> PostBar(BarDto barDto)
{ {
_context.bars.Add(bar); _context.bars.Add(_mapper.Map<Bar>(barDto)) ;
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
return CreatedAtAction("GetBar", new { id = bar.Id }, bar); return CreatedAtAction("GetBar", new { id = barDto.Id }, _mapper.Map<BarDto>(barDto));
} }
// DELETE: api/Bars/5 // DELETE: api/Bars/5

@ -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
{ {
@ -18,11 +21,13 @@ namespace cat_cafe.Controllers
{ {
private readonly CatCafeDbContext _context; private readonly CatCafeDbContext _context;
private readonly IMapper _mapper; private readonly IMapper _mapper;
private readonly ILogger<CatsController> _logger;
public CatsController(CatCafeDbContext context, IMapper mapper) public CatsController(CatCafeDbContext context, IMapper mapper, ILogger<CatsController> logger)
{ {
_mapper = mapper; _mapper = mapper;
_context = context; _context = context;
_logger = logger;
} }
// GET: api/Cats // GET: api/Cats
@ -32,6 +37,7 @@ namespace cat_cafe.Controllers
var cats = await _context.cats.ToListAsync(); var cats = await _context.cats.ToListAsync();
return _mapper.Map<List<CatDto>>(cats); return _mapper.Map<List<CatDto>>(cats);
return Ok(_mapper.Map<List<CatDto>>(cats));
} }
// GET: api/Cats/5 // GET: api/Cats/5
@ -45,7 +51,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

@ -0,0 +1,13 @@
using System;
using cat_cafe.Entities;
namespace cat_cafe.Dto
{
public class BarDto
{
public long Id { get; set; }
public string? Name { get; set; }
public List<CatDto> cats { get; set; } = new List<CatDto>();
}
}

Binary file not shown.

@ -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"; }

@ -0,0 +1,18 @@
using System;
using AutoMapper;
using cat_cafe.Dto;
using cat_cafe.Entities;
namespace cat_cafe.Mappers
{
public class BarMapper:Profile
{
public BarMapper()
{
// var mapper = config.CreateMapper();
CreateMap<Bar, BarDto>().ReverseMap();
}
}
}

@ -8,11 +8,6 @@ using cat_cafe.Entities;
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.
@ -41,18 +36,13 @@ catch (Exception ex) { Console.WriteLine($"{ex.Message}\n... Couldn't use the da
// 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();
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())
{ {
@ -67,4 +57,5 @@ app.UseAuthorization();
app.MapControllers(); app.MapControllers();
Log.Information("program start"); Log.Information("program start");
app.Run(); app.Run();

@ -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

@ -137,3 +137,15 @@
2023-02-04 09:53:40.756 +01:00 [INF] program start 2023-02-04 09:53:40.756 +01:00 [INF] program start
2023-02-04 09:53:50.214 +01:00 [INF] GET => get All customers 2023-02-04 09:53:50.214 +01:00 [INF] GET => get All customers
2023-02-04 09:53:50.290 +01:00 [INF] GET => 200 System.Collections.Generic.List`1[cat_cafe.Entities.Customer] length[3] 2023-02-04 09:53:50.290 +01:00 [INF] GET => 200 System.Collections.Generic.List`1[cat_cafe.Entities.Customer] length[3]
2023-02-04 11:47:11.896 +01:00 [INF] program start
2023-02-04 11:47:24.495 +01:00 [INF] GET => get All customers
2023-02-04 11:47:24.570 +01:00 [INF] GET => 200 System.Collections.Generic.List`1[cat_cafe.Entities.Customer] length[3]
2023-02-04 11:47:45.623 +01:00 [INF] POST => post customer
2023-02-04 11:47:45.710 +01:00 [INF] POST => 201 cat_cafe.Entities.Customer {"Id":4,"FullName":"string","Age":0}
2023-02-04 11:47:47.622 +01:00 [INF] POST => post customer
2023-02-04 11:47:47.627 +01:00 [INF] POST => 201 cat_cafe.Entities.Customer {"Id":5,"FullName":"string","Age":0}
2023-02-04 11:47:57.417 +01:00 [INF] GET => get All customers
2023-02-04 11:47:57.421 +01:00 [INF] GET => 200 System.Collections.Generic.List`1[cat_cafe.Entities.Customer] length[5]
2023-02-04 11:48:05.671 +01:00 [INF] program start
2023-02-04 11:48:15.442 +01:00 [INF] GET => get All customers
2023-02-04 11:48:15.516 +01:00 [INF] GET => 200 System.Collections.Generic.List`1[cat_cafe.Entities.Customer] length[5]

Loading…
Cancel
Save