Added Champions Controller

EF_Entity_DB
Emre KARTAL 2 years ago
parent 87c49b8b24
commit b7670ae160

Binary file not shown.

@ -2,5 +2,6 @@
"ExpandedNodes": [ "ExpandedNodes": [
"" ""
], ],
"SelectedNode": "\\LeagueOfLegends.sln",
"PreviewInSolutionExplorer": false "PreviewInSolutionExplorer": false
} }

@ -10,4 +10,10 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DTO\DTO.csproj" />
<ProjectReference Include="..\Model\Model.csproj" />
<ProjectReference Include="..\StubLib\StubLib.csproj" />
</ItemGroup>
</Project> </Project>

@ -0,0 +1,52 @@
using ApiLol.Mapper;
using DTO;
using Microsoft.AspNetCore.Mvc;
using Model;
using StubLib;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace ApiLol.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ChampionsController : ControllerBase
{
IChampionsManager dataManager = new StubData().ChampionsMgr;
// GET: api/<ValuesController>
[HttpGet]
public async Task<IActionResult> Get()
{
var champions = await dataManager.GetItems(0, await dataManager.GetNbItems()); // Le await va permettre que les lignes suivantes ne s'éxécute pas
return Ok(new { result = champions.Select(c => c.ToDto())});
}
// GET api/<ValuesController>/5
[HttpGet("{name}")]
public IActionResult Get(string name)
{
dataManager.GetItemsByName(name, 0, 1);
return NotFound();
}
// POST api/<ValuesController>
[HttpPost]
public async Task<IActionResult> Post([FromBody] ChampionDto value)
{
//await dataManager.AddItem(value.toModel());
return Ok();
}
// PUT api/<ValuesController>/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}
// DELETE api/<ValuesController>/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
}

@ -1,4 +1,6 @@
using ApiLol.Mapper;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Model;
namespace ApiLol.Controllers namespace ApiLol.Controllers
{ {
@ -21,6 +23,13 @@ namespace ApiLol.Controllers
[HttpGet(Name = "GetWeatherForecast")] [HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get() public IEnumerable<WeatherForecast> Get()
{ {
/*
var champion = new Champion("");
//var dtop = ChampionMapper.ToDto(champion);
var dto = champion.ToDto();*/
return Enumerable.Range(1, 5).Select(index => new WeatherForecast return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{ {
Date = DateTime.Now.AddDays(index), Date = DateTime.Now.AddDays(index),

@ -0,0 +1,16 @@
using DTO;
using Model;
namespace ApiLol.Mapper
{
public static class ChampionMapper
{
public static ChampionDto ToDto(this Champion champion)
{
return new ChampionDto()
{
Name = champion.Name,
};
}
}
}

@ -0,0 +1,7 @@
namespace DTO
{
public class ChampionDto
{
public string Name { get; set; }
}
}

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -15,7 +15,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Stub", "Stub", "{2C607793-B
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StubLib", "StubLib\StubLib.csproj", "{B01D7EF2-2D64-409A-A29A-61FB7BB7A9DB}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StubLib", "StubLib\StubLib.csproj", "{B01D7EF2-2D64-409A-A29A-61FB7BB7A9DB}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ApiLol", "ApiLol\ApiLol.csproj", "{D59C9C7B-9BC2-4601-959D-BFA97E46D017}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ApiLol", "ApiLol\ApiLol.csproj", "{D59C9C7B-9BC2-4601-959D-BFA97E46D017}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DTO", "DTO\DTO.csproj", "{3919E408-EB12-4422-989B-C6ED4816D465}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -43,6 +45,10 @@ Global
{D59C9C7B-9BC2-4601-959D-BFA97E46D017}.Debug|Any CPU.Build.0 = Debug|Any CPU {D59C9C7B-9BC2-4601-959D-BFA97E46D017}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D59C9C7B-9BC2-4601-959D-BFA97E46D017}.Release|Any CPU.ActiveCfg = Release|Any CPU {D59C9C7B-9BC2-4601-959D-BFA97E46D017}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D59C9C7B-9BC2-4601-959D-BFA97E46D017}.Release|Any CPU.Build.0 = Release|Any CPU {D59C9C7B-9BC2-4601-959D-BFA97E46D017}.Release|Any CPU.Build.0 = Release|Any CPU
{3919E408-EB12-4422-989B-C6ED4816D465}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3919E408-EB12-4422-989B-C6ED4816D465}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3919E408-EB12-4422-989B-C6ED4816D465}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3919E408-EB12-4422-989B-C6ED4816D465}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

@ -1,151 +1,151 @@
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Numerics; using System.Numerics;
using System.Text; using System.Text;
namespace Model; namespace Model;
public class Champion : IEquatable<Champion> public class Champion : IEquatable<Champion>
{ {
public string Name public string Name
{ {
get => name; get => name;
private init private init
{ {
if(string.IsNullOrWhiteSpace(value)) if(string.IsNullOrWhiteSpace(value))
{ {
name = "Unknown"; name = "Unknown";
return; return;
} }
name = value; name = value;
} }
} }
private readonly string name = null!; private readonly string name = null!;
public string Bio public string Bio
{ {
get => bio; get => bio;
set set
{ {
if(value == null) if(value == null)
{ {
bio = ""; bio = "";
return; return;
} }
bio = value; bio = value;
} }
} }
private string bio = ""; private string bio = "";
public ChampionClass Class { get; set; } public ChampionClass Class { get; set; }
public string Icon { get; set; } public string Icon { get; set; }
public LargeImage Image { get; set; } public LargeImage Image { get; set; }
public Champion(string name, ChampionClass champClass = ChampionClass.Unknown, string icon = "", string image = "", string bio = "") public Champion(string name, ChampionClass champClass = ChampionClass.Unknown, string icon = "", string image = "", string bio = "")
{ {
Name = name; Name = name;
Class = champClass; Class = champClass;
Icon = icon; Icon = icon;
Image = new LargeImage(image); Image = new LargeImage(image);
Bio = bio; Bio = bio;
Characteristics = new ReadOnlyDictionary<string, int>(characteristics); Characteristics = new ReadOnlyDictionary<string, int>(characteristics);
Skins = new ReadOnlyCollection<Skin>(skins); Skins = new ReadOnlyCollection<Skin>(skins);
} }
public ReadOnlyCollection<Skin> Skins { get; private set; } public ReadOnlyCollection<Skin> Skins { get; private set; }
private List<Skin> skins = new (); private List<Skin> skins = new ();
public ReadOnlyDictionary<string, int> Characteristics { get; private set; } public ReadOnlyDictionary<string, int> Characteristics { get; private set; }
private readonly Dictionary<string, int> characteristics = new Dictionary<string, int>(); private readonly Dictionary<string, int> characteristics = new Dictionary<string, int>();
public ImmutableHashSet<Skill> Skills => skills.ToImmutableHashSet(); public ImmutableHashSet<Skill> Skills => skills.ToImmutableHashSet();
private HashSet<Skill> skills = new HashSet<Skill>(); private HashSet<Skill> skills = new HashSet<Skill>();
internal bool AddSkin(Skin skin) internal bool AddSkin(Skin skin)
{ {
if (skins.Contains(skin)) if (skins.Contains(skin))
return false; return false;
skins.Add(skin); skins.Add(skin);
return true; return true;
} }
internal bool RemoveSkin(Skin skin) internal bool RemoveSkin(Skin skin)
=> skins.Remove(skin); => skins.Remove(skin);
public bool AddSkill(Skill skill) public bool AddSkill(Skill skill)
=> skills.Add(skill); => skills.Add(skill);
public bool RemoveSkill(Skill skill) public bool RemoveSkill(Skill skill)
=> skills.Remove(skill); => skills.Remove(skill);
public void AddCharacteristics(params Tuple<string, int>[] someCharacteristics) public void AddCharacteristics(params Tuple<string, int>[] someCharacteristics)
{ {
foreach(var c in someCharacteristics) foreach(var c in someCharacteristics)
{ {
characteristics[c.Item1] = c.Item2; characteristics[c.Item1] = c.Item2;
} }
} }
public bool RemoveCharacteristics(string label) public bool RemoveCharacteristics(string label)
=> characteristics.Remove(label); => characteristics.Remove(label);
public int? this[string label] public int? this[string label]
{ {
get get
{ {
if(!characteristics.TryGetValue(label, out int value)) return null; if(!characteristics.TryGetValue(label, out int value)) return null;
else return value; else return value;
} }
set set
{ {
if(!value.HasValue) if(!value.HasValue)
{ {
RemoveCharacteristics(label); RemoveCharacteristics(label);
return; return;
} }
characteristics[label] = value.Value; characteristics[label] = value.Value;
} }
} }
public override bool Equals(object? obj) public override bool Equals(object? obj)
{ {
if(ReferenceEquals(obj, null)) return false; if(ReferenceEquals(obj, null)) return false;
if(ReferenceEquals(obj, this)) return true; if(ReferenceEquals(obj, this)) return true;
if(GetType() != obj.GetType()) return false; if(GetType() != obj.GetType()) return false;
return Equals(obj as Champion); return Equals(obj as Champion);
} }
public override int GetHashCode() public override int GetHashCode()
=> Name.GetHashCode() % 997; => Name.GetHashCode() % 997;
public bool Equals(Champion? other) public bool Equals(Champion? other)
=> Name.Equals(other?.Name); => Name.Equals(other?.Name);
public override string ToString() public override string ToString()
{ {
StringBuilder sb = new StringBuilder($"{Name} ({Class})"); StringBuilder sb = new StringBuilder($"{Name} ({Class})");
if(!string.IsNullOrWhiteSpace(bio)) if(!string.IsNullOrWhiteSpace(bio))
{ {
sb.AppendLine($"\t{bio}"); sb.AppendLine($"\t{bio}");
} }
if(characteristics.Any()) if(characteristics.Any())
{ {
sb.AppendLine("\tCharacteristics:"); sb.AppendLine("\tCharacteristics:");
foreach(var characteristic in characteristics) foreach(var characteristic in characteristics)
{ {
sb.AppendLine($"\t\t{characteristic.Key} - {characteristic.Value}"); sb.AppendLine($"\t\t{characteristic.Key} - {characteristic.Value}");
} }
} }
if(skills.Any()) if(skills.Any())
{ {
sb.AppendLine("\tSkills:"); sb.AppendLine("\tSkills:");
foreach(var skill in Skills) foreach(var skill in Skills)
{ {
sb.AppendLine($"\t\t{skill.Name} - {skill.Description}"); sb.AppendLine($"\t\t{skill.Name} - {skill.Description}");
} }
} }
return sb.ToString(); return sb.ToString();
} }
} }

Loading…
Cancel
Save