correction routes users

pull/6/head
Kevin MONDEJAR 3 weeks ago
parent 48a15425c3
commit 019ea01797

@ -1,5 +1,6 @@
using Entity; using Entity;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Shared; using Shared;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -86,5 +87,11 @@ namespace Contextlib
{ {
return await _context.images.CountAsync(); return await _context.images.CountAsync();
} }
public async Task<Images?> GetImageByPath(string path)
{
var image = _repository.GetItems(item => item.ImgPath == path,0,1).FirstOrDefault();
return image;
}
} }
} }

@ -54,7 +54,6 @@ namespace Contextlib
{ {
List<Quote> quotes = await _context.quotes.Where(item => item.IsValid && item.Langage == (LangEnum)lang) List<Quote> quotes = await _context.quotes.Where(item => item.IsValid && item.Langage == (LangEnum)lang)
.Include(q => q.Source).Include(q => q.Character).ThenInclude(c => c.Images).Include(q => q.Favorite) .Include(q => q.Source).Include(q => q.Character).ThenInclude(c => c.Images).Include(q => q.Favorite)
<<<<<<< HEAD
.ToListAsync(); .ToListAsync();
Quote quote = quotes[date.DayNumber % quotes.Count()]; Quote quote = quotes[date.DayNumber % quotes.Count()];
@ -63,12 +62,6 @@ namespace Contextlib
.FirstOrDefaultAsync() ?? quotes.First();*/ .FirstOrDefaultAsync() ?? quotes.First();*/
//Quote quote = _repo.GetById(date.DayNumber % quotes.Count()) ?? quotes.First(); //Quote quote = _repo.GetById(date.DayNumber % quotes.Count()) ?? quotes.First();
=======
.ToList();
if (quotes.Count() == 0) return null;
Quote quote = quotes[ date.DayNumber % quotes.Count() ] ;
>>>>>>> 8471e6cfd50227037555f6650ed659ac091cc613
return quote; return quote;
} }

@ -4,6 +4,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Entity; using Entity;
using Microsoft.EntityFrameworkCore;
using Shared; using Shared;
namespace Contextlib namespace Contextlib
@ -23,9 +24,20 @@ namespace Contextlib
public async Task AddUser(Users user) public async Task AddUser(Users user)
{ {
/*if (await ExistEmail(user.Email) || (await ExistUsername(user.UserName)))
{
throw new ArgumentException("The given Email or Username Already exist");
}*/
if (user == null) { if (user == null) {
throw new ArgumentNullException(nameof(user), "user cannot be null."); throw new ArgumentNullException(nameof(user), "user cannot be null.");
} }
var dbI = new DbImagesManager(_context);
var image = await dbI.GetImageByPath(user.Images.ImgPath);
if(image != null)
{
user.IdImage = image.Id;
user.Images = image;
}
_repo.Insert(user); _repo.Insert(user);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
} }
@ -37,13 +49,13 @@ namespace Contextlib
public async Task<bool> ExistEmail(string email) public async Task<bool> ExistEmail(string email)
{ {
IQueryable<Users> users = _context.users.Where(u=>u.Email == email); Users? users = await _context.users.Where(u=>u.Email == email).FirstOrDefaultAsync();
return users != null; return users != null;
} }
public async Task<bool> ExistUsername(string username) public async Task<bool> ExistUsername(string username)
{ {
IQueryable<Users> users = _context.users.Where(u => u.UserName == username); Users? users = await _context.users.Where(u => u.UserName == username).FirstOrDefaultAsync();
return users != null; return users != null;
} }
@ -56,7 +68,7 @@ namespace Contextlib
public async Task<string> GetHashPassword(string username) public async Task<string> GetHashPassword(string username)
{ {
Users? user = _context.users.Where(u=>u.UserName == username).FirstOrDefault(); Users? user = _context.users.Where(u=>u.UserName == username).FirstOrDefault();
if (user != null) if (user == null)
{ {
return ""; return "";
} }
@ -130,14 +142,19 @@ namespace Contextlib
public async Task<Users> UpdateUser(int userId, Users user) public async Task<Users> UpdateUser(int userId, Users user)
{ {
Users? u = _repo.GetById(userId); Users? u = _repo.GetById(userId, item => item.Id == userId, nameof(Users.Images));
if (u != null) if (u != null)
{ {
bool change = false; bool change = false;
if (user.IdImage != 0) if (user.Images.ImgPath != null)
{ {
u.IdImage = user.IdImage; var dbI = new DbImagesManager(_context);
change = true; var img = await dbI.GetImageByPath(user.Images.ImgPath);
if (img != null)
{
u.IdImage = dbI.GetImageByPath(user.Images.ImgPath).Id;
change = true;
}
} }
if (user.UserName != null) if (user.UserName != null)
{ {

@ -206,7 +206,7 @@ namespace Dto2Entities
user.Password = item.Password; user.Password = item.Password;
user.Email = item.Email; user.Email = item.Email;
user.date = item.Created; user.date = item.Created;
user.ImageProfil = item.Images.ImgPath; user.ImageProfil = item.Images.ImgPath; // image null avec put
return user; return user;
} }
@ -436,6 +436,7 @@ namespace Dto2Entities
user.Password = item.Password; user.Password = item.Password;
user.Email = item.Email; user.Email = item.Email;
user.Created = item.date; user.Created = item.date;
user.Images = new Images();
user.Images.ImgPath = item.ImageProfil; user.Images.ImgPath = item.ImageProfil;
return user; return user;
} }

@ -67,7 +67,14 @@ namespace ServicesApi
public async Task<UserDTO> GetUserById(int id) public async Task<UserDTO> GetUserById(int id)
{ {
return userService.GetUserById(id).Result.ToDto(); try
{
return (await userService.GetUserById(id)).ToDto();
}
catch(KeyNotFoundException e)
{
throw new KeyNotFoundException(e.Message);
}
} }
public async Task<UserDTO> GetUserByUsername(string username) public async Task<UserDTO> GetUserByUsername(string username)

@ -0,0 +1,22 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace StubbedContextLib.Migrations
{
/// <inheritdoc />
public partial class migration204 : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}

@ -574,21 +574,23 @@ namespace WfApi.Controllers
if (newUser == null) if (newUser == null)
{ {
return BadRequest(new { message = "User data is required." }); return BadRequest(new { message = "User data is required." });
} }
var existingPlayer = _user.GetUserById(newUser.Id).Result; try
if (existingPlayer != null)
{ {
var existingPlayer = await _user.GetUserById(newUser.Id);
return Conflict(new { message = "A user with this ID already exists." }); return Conflict(new { message = "A user with this ID already exists." });
} }
catch(KeyNotFoundException e)
{
_user.AddUser(newUser);
_user.AddUser(newUser); return CreatedAtAction(nameof(GetAllUsers), new { id = newUser.Id }, newUser);
}
return CreatedAtAction(nameof(GetAllUsers), new { id = newUser.Id }, newUser);
} }
catch (Exception) catch (Exception e)
{ {
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Erreur interne du serveur." }); return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Erreur interne du serveur." + e.Message });
} }
} }

Loading…
Cancel
Save