Changing Dbcontext/Manager from Singleton to Scoped
continuous-integration/drone/push Build is failing Details

pull/4/head
Arthur VALIN 2 years ago
parent 8cecd77d44
commit af54cf8375

@ -4,7 +4,6 @@ using DTO;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Model; using Model;
using StubLib; using StubLib;
using System.Text.Json;
using System.Xml.Linq; using System.Xml.Linq;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
@ -43,9 +42,9 @@ namespace API_LoL_Project.Controllers
return BadRequest("No chamions found with Id "); return BadRequest("No chamions found with Id ");
} }
_logger.LogInformation("Executing {Action} with parameters: {Parameters}", nameof(Get), request);; _logger.LogInformation("Executing {Action} with parameters: {Parameters}", nameof(Get), request);;
var champions = await dataManager.GetItems(request.PageNumber, totalcount, request.orderingPropertyName, (request.descending == null ? false : (bool)request.descending)); var champions = await dataManager.GetItems(request.index, request.count, request.orderingPropertyName, request.descending);
IEnumerable<ChampionDTO> res = champions.Select(c => c.toDTO()); IEnumerable<ChampionDTO> res = champions.Select(c => c.ToDTO());
if (res.Count() >= 0 || res == null) if (res.Count() <= 0 || res == null)
{ {
_logger.LogWarning("No chamions found with Id"); _logger.LogWarning("No chamions found with Id");
return BadRequest("No chamions found with Id "); return BadRequest("No chamions found with Id ");
@ -73,7 +72,7 @@ namespace API_LoL_Project.Controllers
var champion = await dataManager var champion = await dataManager
.GetItemsByName(name, 0, await dataManager.GetNbItems()); .GetItemsByName(name, 0, await dataManager.GetNbItems());
_logger.LogInformation("Executing {Action} with name : {championName}", nameof(GetChampionsByName), name); _logger.LogInformation("Executing {Action} with name : {championName}", nameof(GetChampionsByName), name);
ChampionDTO res = champion.First().toDTO(); ChampionDTO res = champion.First().ToDTO();
if (res == null) if (res == null)
{ {
_logger.LogWarning("No chamions found with {name}", name); ; _logger.LogWarning("No chamions found with {name}", name); ;
@ -96,7 +95,7 @@ namespace API_LoL_Project.Controllers
{ {
try try
{ {
var newChampion = value.toModel(); var newChampion = value.ToModel();
await dataManager.AddItem(newChampion); await dataManager.AddItem(newChampion);
return CreatedAtAction(nameof(Get), newChampion) ; return CreatedAtAction(nameof(Get), newChampion) ;
} }
@ -118,7 +117,7 @@ namespace API_LoL_Project.Controllers
{ {
var champion = await dataManager var champion = await dataManager
.GetItemsByName(name, 0, await dataManager.GetNbItems()); .GetItemsByName(name, 0, await dataManager.GetNbItems());
await dataManager.UpdateItem(champion.First(), value.toModel()); await dataManager.UpdateItem(champion.First(), value.ToModel());
return Ok(); return Ok();
} }
catch(Exception e) catch(Exception e)

@ -2,28 +2,9 @@
{ {
public class PageRequest public class PageRequest
{ {
//max leght
public string? orderingPropertyName { get; set; } = null; public string? orderingPropertyName { get; set; } = null;
public bool? descending { get; set; } = false; public bool descending { get; set; } = false;
const int maxPageSize = 50;
public int PageNumber { get; set; } = 1;
public int index { get; set; } = 1; public int index { get; set; } = 1;
public int count { get; set; } = 1; public int count { get; set; } = 1;
//max lentght
private int _pageSize;
public int PageSize
{
get
{
return _pageSize;
}
set
{
_pageSize = (value > maxPageSize) ? maxPageSize : value;
}
}
} }
} }

@ -12,7 +12,7 @@ namespace API_LoL_Project.Controllers
[ApiController] [ApiController]
public class RuneController : ControllerBase public class RuneController : ControllerBase
{ {
public IRunesManager runesManager; /*public IRunesManager runesManager;
// you should create a custom logger to be prety // you should create a custom logger to be prety
private readonly ILogger<RuneController> _logger; private readonly ILogger<RuneController> _logger;
@ -26,7 +26,7 @@ namespace API_LoL_Project.Controllers
/*// GET: api/<RuneController> *//*// GET: api/<RuneController>
[HttpGet] [HttpGet]
public async Task<IEnumerable<RuneDTO>> Get() public async Task<IEnumerable<RuneDTO>> Get()
{ {
@ -42,7 +42,7 @@ namespace API_LoL_Project.Controllers
return BadRequest(e.Message); return BadRequest(e.Message);
} }
}*/ }*//*
// GET: api/<RuneController> // GET: api/<RuneController>
[HttpGet] [HttpGet]
@ -127,6 +127,6 @@ namespace API_LoL_Project.Controllers
[HttpDelete("{id}")] [HttpDelete("{id}")]
public void Delete(int id) public void Delete(int id)
{ {
} }*/
} }
} }

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc; using DTO;
using Microsoft.AspNetCore.Mvc;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
@ -8,7 +9,7 @@ namespace API_LoL_Project.Controllers
[ApiController] [ApiController]
public class RunePageController : ControllerBase public class RunePageController : ControllerBase
{ {
// GET: api/<RunePageController> /* // GET: api/<RunePageController>
[HttpGet] [HttpGet]
public async Task<ActionResult<IEnumerable<RuneDTO>>> Get([FromQuery] Request.PageRequest request) public async Task<ActionResult<IEnumerable<RuneDTO>>> Get([FromQuery] Request.PageRequest request)
{ {
@ -65,6 +66,6 @@ namespace API_LoL_Project.Controllers
[HttpDelete("{id}")] [HttpDelete("{id}")]
public void Delete(int id) public void Delete(int id)
{ {
} }*/
} }
} }

@ -8,7 +8,7 @@ namespace API_LoL_Project.Controllers
[ApiController] [ApiController]
public class SkillController : ControllerBase public class SkillController : ControllerBase
{ {
// GET: api/<SkillController> /* // GET: api/<SkillController>
[HttpGet] [HttpGet]
public async Task<ActionResult<IEnumerable<RuneDTO>>> Get([FromQuery] Request.PageRequest request) public async Task<ActionResult<IEnumerable<RuneDTO>>> Get([FromQuery] Request.PageRequest request)
{ {
@ -65,6 +65,6 @@ namespace API_LoL_Project.Controllers
[HttpDelete("{id}")] [HttpDelete("{id}")]
public void Delete(int id) public void Delete(int id)
{ {
} }*/
} }
} }

@ -8,7 +8,7 @@ namespace API_LoL_Project.Controllers
[ApiController] [ApiController]
public class SkinController : ControllerBase public class SkinController : ControllerBase
{ {
// GET: api/<SkinController> /* // GET: api/<SkinController>
[HttpGet] [HttpGet]
public async Task<ActionResult<IEnumerable<RuneDTO>>> Get([FromQuery] Request.PageRequest request) public async Task<ActionResult<IEnumerable<RuneDTO>>> Get([FromQuery] Request.PageRequest request)
{ {
@ -65,6 +65,6 @@ namespace API_LoL_Project.Controllers
[HttpDelete("{id}")] [HttpDelete("{id}")]
public void Delete(int id) public void Delete(int id)
{ {
} }*/
} }
} }

@ -43,10 +43,9 @@ namespace API_LoL_Project.Mapper
{ {
Name = item.Name, Name = item.Name,
Bio = item.Bio, Bio = item.Bio,
Characteristics = item.Characteristics,
skills = item.Skills, skills = item.Skills,
skins = item.Skins, skins = item.Skins,
LargeImage = item.Image LargeImage = item.Image.Base64
}; };

@ -1,29 +1,31 @@
namespace API_LoL_Project.Mapper namespace API_LoL_Project.Mapper
{ {
using DTO; using DTO;
using Entities;
using Model; using Model;
namespace API_LoL_Project.Mapper namespace API_LoL_Project.Mapper
{ {
public static RuneDTO ToDTO(this Rune item) public static class RuneMapper
{ {
public static RuneDTO ToDTO(this Rune item)
return new RuneDTO()
{ {
Name = item.Name,
Family = item.Family,
};
}
public static Rune ToModel(this RuneDTO dto) return new RuneDTO()
{ {
/*if (dto == null) Name = item.Name,
Family = item.Family,
};
}
public static Rune ToModel(this RuneDTO dto)
{ {
*//* var message = string.Format("Champion with name = {} not found", dto.Name); /*if (dto == null)
throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, message));*//* {
}*/ *//* var message = string.Format("Champion with name = {} not found", dto.Name);
return new Rune(dto.Name, dto.Family); throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, message));*//*
}*/
return new Rune(dto.Name, dto.Family);
}
} }
} }
} }

@ -10,4 +10,4 @@ namespace API_LoL_Project.Mapper
} }
} }
}

@ -1,19 +1,25 @@
using Business; using Business;
using Entities;
using Microsoft.EntityFrameworkCore;
using Model; using Model;
using StubLib; using StubLib;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
// Add services to the container. var connectionString = builder.Configuration.GetConnectionString("LolDatabase");
builder.Services.AddDbContext<LolDbContext>(options =>
options.UseSqlite(connectionString), ServiceLifetime.Singleton);
builder.Services.AddControllers(); builder.Services.AddControllers();
// 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.AddSingleton<IDataManager, DbData>(); builder.Services.AddSingleton<IDataManager, DbData>();
//builder.Services.AddSingleton<IDataManager, StubData>();
var app = builder.Build(); var app = builder.Build();
app?.Services?.GetService<LolDbContext>()?.Database.EnsureCreated();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())

@ -6,6 +6,6 @@
} }
}, },
"ConnectionStrings": { "ConnectionStrings": {
"LolDatabase": "Data Source=Entities.Champions.db" "LolDatabase": "Data Source=Entities.LolDatabase.db"
} }
} }

@ -3,9 +3,9 @@
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Information",
"Microsoft.AspNetCore": "Warning" "Microsoft.AspNetCore": "Warning"
},
"ConnectionStrings": {
"LolDatabase": "Data Source=Entities.Champions.db"
} }
},
"ConnectionStrings": {
"LolDatabase": "Data Source=Entities.LolDatabase.db"
} }
} }

@ -6,7 +6,7 @@
} }
}, },
"ConnectionStrings": { "ConnectionStrings": {
"LolDatabase": "Data Source=Entities.Champions.db" "LolDatabase": "Data Source=Entities.LolDatabase.db"
}, },
"AllowedHosts": "*" "AllowedHosts": "*"
} }

@ -27,6 +27,7 @@ namespace Business
public async Task<IEnumerable<Champion?>> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) public async Task<IEnumerable<Champion?>> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false)
{ {
Console.WriteLine("GET");
return parent.DbContext.champions.GetItemsWithFilterAndOrdering( return parent.DbContext.champions.GetItemsWithFilterAndOrdering(
c => true, c => true,
index, count, index, count,

@ -6,7 +6,6 @@ namespace Business
{ {
public partial class DbData : IDataManager public partial class DbData : IDataManager
{ {
public DbData(LolDbContext dbContext) public DbData(LolDbContext dbContext)
{ {
DbContext = dbContext; DbContext = dbContext;
@ -15,6 +14,7 @@ namespace Business
RunesMgr = new RunesManager(this); RunesMgr = new RunesManager(this);
RunePagesMgr = new RunePagesManager(this); RunePagesMgr = new RunePagesManager(this);
} }
protected LolDbContext DbContext{ get; } protected LolDbContext DbContext{ get; }
public IChampionsManager ChampionsMgr { get; } public IChampionsManager ChampionsMgr { get; }

@ -22,7 +22,7 @@ namespace Business
: temp.OrderBy(item => prop.GetValue(item)); : temp.OrderBy(item => prop.GetValue(item));
} }
} }
return temp.Skip(index * count).Take(count) return temp.Skip(index * count).Take(count);
} }
} }
} }

@ -1,4 +1,5 @@
using Model; using Model;
using System.Buffers.Text;
namespace DTO namespace DTO
{ {
@ -14,7 +15,7 @@ namespace DTO
public string Name { get; set; } public string Name { get; set; }
public string Bio { get; set; } public string Bio { get; set; }
public string Characteristics { get; set; } public string Characteristics { get; set; }
public byte[] LargeImage { get; set; } public string LargeImage { get; set; }
public IEnumerable<Skin> skins { get; set; } public IEnumerable<Skin> skins { get; set; }
public IEnumerable<Skill> skills { get; set; } public IEnumerable<Skill> skills { get; set; }

@ -1,4 +1,5 @@
using Model; using Model;
using Shared;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;

@ -21,6 +21,7 @@ namespace Entities
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{ {
if (!optionsBuilder.IsConfigured) { if (!optionsBuilder.IsConfigured) {
Console.WriteLine("!IsConfigured...");
optionsBuilder.UseSqlite($"Data Source=Entities.Champions.db"); optionsBuilder.UseSqlite($"Data Source=Entities.Champions.db");
} }
} }

@ -7,7 +7,7 @@ ChampionEntity imri = new()
Bio = "Fou Furieux", Bio = "Fou Furieux",
Class = ChampionClass.Assassin Class = ChampionClass.Assassin
}; };
using (var context = new LolDbContext()) using (var context = new LolDbContext(null))
{ {
// Crée des nounours et les insère dans la base // Crée des nounours et les insère dans la base
Console.WriteLine("Creates and inserts new Champion"); Console.WriteLine("Creates and inserts new Champion");

@ -19,7 +19,7 @@ namespace EntityMapper
public static Champion ToModel(this ChampionEntity entity) public static Champion ToModel(this ChampionEntity entity)
{ {
return new(entity.Name, entity.Class, entity.Icon, entity.Image.Base64, entity.Bio); return new(entity.Name, entity.Class, entity.Icon, "", entity.Bio);
} }
} }

@ -3,21 +3,21 @@ using Model;
namespace EntityMapper namespace EntityMapper
{ {
public static class RuneMapper public static class RuneMapper
{
public static RuneEntity ToEntity(this Rune item)
{ {
public static RuneEntity ToEntity(this Rune item) throw new NotImplementedException();
{ }
throw new NotImplementedException();
}
public static Rune ToModel(this RuneEntity entity)
{
throw new NotImplementedException();
} public static Rune ToModel(this RuneEntity entity)
{
throw new NotImplementedException();
} }
} }
} }

@ -19,4 +19,3 @@ namespace EntityMapper
} }
} }
}

@ -0,0 +1,7 @@
{
"profiles": {
"Model": {
"commandName": "Project"
}
}
}
Loading…
Cancel
Save