Files
FoodsharingOnboarding/FoodsharingSiegen.Server/Data/Service/UserService.cs
2022-05-31 15:08:55 +02:00

159 lines
5.6 KiB
C#

using FoodsharingSiegen.Contracts;
using FoodsharingSiegen.Contracts.Entity;
using FoodsharingSiegen.Server.Auth;
using Microsoft.EntityFrameworkCore;
namespace FoodsharingSiegen.Server.Data.Service
{
/// <summary>
/// The user service class (a. beging, 11.04.2022)
/// </summary>
/// <seealso cref="ServiceBase"/>
public class UserService : ServiceBase
{
#region Setup/Teardown
/// <summary>
/// Initializes a new instance of the <see cref="UserService"/> class
/// </summary>
/// <param name="context">The context</param>
/// <param name="authService"></param>
public UserService(FsContext context, AuthService authService) : base(context, authService) { }
#endregion
#region Public Method AddUserAsync
/// <summary>
/// Adds the user using the specified user (a. beging, 11.04.2022)
/// </summary>
/// <param name="user">The user</param>
/// <returns>A task containing an operation result of user</returns>
public async Task<OperationResult<User>> AddUserAsync(User user)
{
try
{
if (await Context.Users!.AnyAsync(x => x.Mail.ToLower().Equals(user.Mail.ToLower())))
return new OperationResult<User>(new Exception("Diese E-Mail Adresse wird bereits verwendet"));
user.Created = DateTime.UtcNow;
if (string.IsNullOrWhiteSpace(user.Password))
user.Password = string.Empty;
await Context.Users!.AddAsync(user);
var saveResult = await Context.SaveChangesAsync();
if (saveResult == 0) return new OperationResult<User>(new Exception("Fehler bei der Registrierung"));
return new OperationResult<User>(user);
}
catch (Exception e)
{
return new OperationResult<User>(e);
}
}
#endregion
#region Public Method GetUsersAsync
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Gets users asynchronous. </summary>
///
/// <remarks> A Beging, 20.10.2021. </remarks>
///
/// <returns> An asynchronous result that yields the users. </returns>
////////////////////////////////////////////////////////////////////////////////////////////////////
public async Task<OperationResult<List<User>>> GetUsersAsync()
{
try
{
var users = await Context.Users!.AsNoTracking().ToListAsync();
return new OperationResult<List<User>>(users);
}
catch (Exception e)
{
return new OperationResult<List<User>>(e);
}
}
#endregion
#region Public Method SetPassword
/// <summary>
/// Sets the password using the specified user (a. beging, 20.05.2022)
/// </summary>
/// <param name="user">The user</param>
/// <returns>A task containing the operation result</returns>
public async Task<OperationResult> SetPassword(User user)
{
try
{
var entityUser = await Context.Users!.FirstOrDefaultAsync(x => x.Id == user.Id);
if (entityUser == null) return new OperationResult(new Exception("User not found"));
entityUser.Password = user.Password;
var saveR = await Context.SaveChangesAsync();
if(saveR < 1) return new OperationResult(new Exception("Fehler beim Speichern"));
return new OperationResult();
}
catch (Exception e)
{
return new OperationResult(e);
}
}
#endregion
#region Public Method Update
/// <summary>
/// Updates the user (a. beging, 11.04.2022)
/// </summary>
/// <param name="user">The user</param>
/// <returns>A task containing the operation result</returns>
public async Task<OperationResult> Update(User user)
{
try
{
var entityUser = await Context.Users!.FirstOrDefaultAsync(x => x.Id == user.Id);
if (entityUser == null) return new OperationResult(new Exception("User not found"));
if (entityUser.Mail != user.Mail ||
entityUser.Verified != user.Verified ||
entityUser.Type != user.Type ||
entityUser.Groups != user.Groups)
{
entityUser.ForceLogout = true;
}
entityUser.Memo = user.Memo;
entityUser.Mail = user.Mail;
entityUser.Name = user.Name;
entityUser.Type = user.Type;
entityUser.Verified = user.Verified;
entityUser.Groups = user.Groups;
entityUser.Network = user.Network;
if (!Context.HasChanges())
return new OperationResult(new Exception("Nichts zum Speichern gefunden"));
var saveR = await Context.SaveChangesAsync();
if(saveR < 1) return new OperationResult(new Exception("Fehler beim Speichern"));
return new OperationResult();
}
catch (Exception e)
{
return new OperationResult(e);
}
}
#endregion
}
}