Skip to content

Commit

Permalink
Restored GetCurrentUser method
Browse files Browse the repository at this point in the history
  • Loading branch information
mkoper02 committed Jun 19, 2024
1 parent caa737a commit 551aadb
Showing 1 changed file with 55 additions and 15 deletions.
70 changes: 55 additions & 15 deletions Server/ReasnAPI/ReasnAPI/Services/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,59 @@
using ReasnAPI.Models.Database;
using ReasnAPI.Models.DTOs;
using System.Linq.Expressions;
using System.Security.Claims;
using System.Transactions;

namespace ReasnAPI.Services;

public class UserService(ReasnContext context)
public class UserService
{
private readonly ReasnContext _context;
private readonly IHttpContextAccessor _httpContextAccessor;

public UserService(ReasnContext context)

Check warning on line 17 in Server/ReasnAPI/ReasnAPI/Services/UserService.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (ubuntu-latest)

Non-nullable field '_httpContextAccessor' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 17 in Server/ReasnAPI/ReasnAPI/Services/UserService.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (macos-latest)

Non-nullable field '_httpContextAccessor' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 17 in Server/ReasnAPI/ReasnAPI/Services/UserService.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (windows-latest)

Non-nullable field '_httpContextAccessor' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
{
_context = context;
}

public UserService(ReasnContext context, IHttpContextAccessor httpContextAccessor)
{
_context = context;
_httpContextAccessor = httpContextAccessor;
}

public User GetCurrentUser()
{
var httpContext = _httpContextAccessor.HttpContext;

if (httpContext is null)
{
throw new InvalidOperationException("No HTTP context available");
}

var email = httpContext.User.FindFirstValue(ClaimTypes.Email);
if (string.IsNullOrEmpty(email))
{
throw new UnauthorizedAccessException("No email claim found in token");
}

var user = _context.Users.FirstOrDefault(u => u.Email == email);

if (user is null)
{
throw new NotFoundException("User associated with email not found");
}

return user;
}

public UserDto UpdateUser(int userId, UserDto userDto)
{
using (var scope = new TransactionScope())
{
ArgumentNullException.ThrowIfNull(userDto);

var user = context.Users
var user = _context.Users
.Include(u => u.UserInterests)
.ThenInclude(ui => ui.Interest)
.FirstOrDefault(r => r.Id == userId);
Expand All @@ -26,21 +66,21 @@ public UserDto UpdateUser(int userId, UserDto userDto)
throw new NotFoundException("User not found");
}

var usernameExists = context.Users.Any(r => r.Username == userDto.Username && r.Id != userId);
var usernameExists = _context.Users.Any(r => r.Username == userDto.Username && r.Id != userId);

if (usernameExists)
{
throw new BadRequestException("User with given username already exists");
}

var emailExists = context.Users.Any(r => r.Email == userDto.Email && r.Id != userId);
var emailExists = _context.Users.Any(r => r.Email == userDto.Email && r.Id != userId);

if (emailExists)
{
throw new BadRequestException("User with given email already exists");
}

var phoneExists = context.Users.Any(r => r.Phone == userDto.Phone && r.Id != userId);
var phoneExists = _context.Users.Any(r => r.Phone == userDto.Phone && r.Id != userId);

if (phoneExists)
{
Expand All @@ -57,26 +97,26 @@ public UserDto UpdateUser(int userId, UserDto userDto)
user.AddressId = userDto.AddressId;
user.UpdatedAt = DateTime.UtcNow;

context.Users.Update(user);
_context.Users.Update(user);

if (userDto.Interests is null || userDto.Interests.Count == 0)
{
context.SaveChanges();
_context.SaveChanges();
scope.Complete();
return userDto;
}

var interestsToRemove = user.UserInterests
.Where(ui => !userDto.Interests.Exists(uid => uid.Interest.Name == ui.Interest.Name));

context.UserInterests.RemoveRange(interestsToRemove);
_context.UserInterests.RemoveRange(interestsToRemove);

var interestsToAdd = userDto.Interests
.Where(uid => !user.UserInterests.Any(ui => ui.Interest.Name == uid.Interest.Name))
.Select(uid => uid.ToEntity())
.ToList();

context.UserInterests.AddRange(interestsToAdd);
_context.UserInterests.AddRange(interestsToAdd);

var interestsToUpdate = user.UserInterests
.Where(ui => userDto.Interests.Exists(uid => uid.Interest.Name == ui.Interest.Name))
Expand All @@ -92,10 +132,10 @@ public UserDto UpdateUser(int userId, UserDto userDto)
}

interest.Level = updatedInterest.Level;
context.UserInterests.Update(interest);
_context.UserInterests.Update(interest);
}

context.SaveChanges();
_context.SaveChanges();
scope.Complete();
}

Expand All @@ -104,7 +144,7 @@ public UserDto UpdateUser(int userId, UserDto userDto)

public UserDto GetUserById(int userId)
{
var user = context.Users
var user = _context.Users
.Include(u => u.UserInterests)
.FirstOrDefault(u => u.Id == userId);

Expand All @@ -118,7 +158,7 @@ public UserDto GetUserById(int userId)

public UserDto GetUserByUsername(string username)
{
var user = context.Users
var user = _context.Users
.Include(u => u.UserInterests)
.FirstOrDefault(u => u.Username == username);

Expand All @@ -132,7 +172,7 @@ public UserDto GetUserByUsername(string username)

public IEnumerable<UserDto> GetUsersByFilter(Expression<Func<User, bool>> filter)
{
return context.Users
return _context.Users
.Include(u => u.UserInterests)
.ThenInclude(ui => ui.Interest)
.Where(filter)
Expand All @@ -142,7 +182,7 @@ public IEnumerable<UserDto> GetUsersByFilter(Expression<Func<User, bool>> filter

public IEnumerable<UserDto> GetAllUsers()
{
var users = context.Users
var users = _context.Users
.Include(u => u.UserInterests)
.ThenInclude(ui => ui.Interest)
.ToList();
Expand Down

0 comments on commit 551aadb

Please sign in to comment.