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 Model;
using StubLib;
using System.Text.Json;
using System.Xml.Linq;
// 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 ");
}
_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));
IEnumerable<ChampionDTO> res = champions.Select(c => c.toDTO());
if (res.Count() >= 0 || res == null)
var champions = await dataManager.GetItems(request.index, request.count, request.orderingPropertyName, request.descending);
IEnumerable<ChampionDTO> res = champions.Select(c => c.ToDTO());
if (res.Count() <= 0 || res == null)
{
_logger.LogWarning("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
.GetItemsByName(name, 0, await dataManager.GetNbItems());
_logger.LogInformation("Executing {Action} with name : {championName}", nameof(GetChampionsByName), name);
ChampionDTO res = champion.First().toDTO();
ChampionDTO res = champion.First().ToDTO();
if (res == null)
{
_logger.LogWarning("No chamions found with {name}", name); ;
@ -96,7 +95,7 @@ namespace API_LoL_Project.Controllers
{
try
{
var newChampion = value.toModel();
var newChampion = value.ToModel();
await dataManager.AddItem(newChampion);
return CreatedAtAction(nameof(Get), newChampion) ;
}
@ -118,7 +117,7 @@ namespace API_LoL_Project.Controllers
{
var champion = await dataManager
.GetItemsByName(name, 0, await dataManager.GetNbItems());
await dataManager.UpdateItem(champion.First(), value.toModel());
await dataManager.UpdateItem(champion.First(), value.ToModel());
return Ok();
}
catch(Exception e)

@ -2,28 +2,9 @@
{
public class PageRequest
{
//max leght
public string? orderingPropertyName { get; set; } = null;
public bool? descending { get; set; } = false;
const int maxPageSize = 50;
public int PageNumber { get; set; } = 1;
public bool descending { get; set; } = false;
public int index { 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]
public class RuneController : ControllerBase
{
public IRunesManager runesManager;
/*public IRunesManager runesManager;
// you should create a custom logger to be prety
private readonly ILogger<RuneController> _logger;
@ -26,7 +26,7 @@ namespace API_LoL_Project.Controllers
/*// GET: api/<RuneController>
*//*// GET: api/<RuneController>
[HttpGet]
public async Task<IEnumerable<RuneDTO>> Get()
{
@ -42,7 +42,7 @@ namespace API_LoL_Project.Controllers
return BadRequest(e.Message);
}
}*/
}*//*
// GET: api/<RuneController>
[HttpGet]
@ -127,6 +127,6 @@ namespace API_LoL_Project.Controllers
[HttpDelete("{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
@ -8,7 +9,7 @@ namespace API_LoL_Project.Controllers
[ApiController]
public class RunePageController : ControllerBase
{
// GET: api/<RunePageController>
/* // GET: api/<RunePageController>
[HttpGet]
public async Task<ActionResult<IEnumerable<RuneDTO>>> Get([FromQuery] Request.PageRequest request)
{
@ -65,6 +66,6 @@ namespace API_LoL_Project.Controllers
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}*/
}
}

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

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

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

@ -1,29 +1,31 @@
namespace API_LoL_Project.Mapper
{
using DTO;
using Entities;
using Model;
namespace API_LoL_Project.Mapper
{
public static RuneDTO ToDTO(this Rune item)
public static class RuneMapper
{
return new RuneDTO()
public static RuneDTO ToDTO(this Rune item)
{
Name = item.Name,
Family = item.Family,
};
}
public static Rune ToModel(this RuneDTO dto)
{
/*if (dto == null)
return new RuneDTO()
{
Name = item.Name,
Family = item.Family,
};
}
public static Rune ToModel(this RuneDTO dto)
{
*//* var message = string.Format("Champion with name = {} not found", dto.Name);
throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, message));*//*
}*/
return new Rune(dto.Name, dto.Family);
/*if (dto == null)
{
*//* var message = string.Format("Champion with name = {} not found", dto.Name);
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 Entities;
using Microsoft.EntityFrameworkCore;
using Model;
using StubLib;
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();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddSingleton<IDataManager, DbData>();
//builder.Services.AddSingleton<IDataManager, StubData>();
var app = builder.Build();
app?.Services?.GetService<LolDbContext>()?.Database.EnsureCreated();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())

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

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

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

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

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

@ -22,7 +22,7 @@ namespace Business
: 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 System.Buffers.Text;
namespace DTO
{
@ -14,7 +15,7 @@ namespace DTO
public string Name { get; set; }
public string Bio { 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<Skill> skills { get; set; }

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

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

@ -7,7 +7,7 @@ ChampionEntity imri = new()
Bio = "Fou Furieux",
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
Console.WriteLine("Creates and inserts new Champion");

@ -19,7 +19,7 @@ namespace EntityMapper
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
{
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();
}
}
}
}

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