using FoodsharingSiegen.Contracts;
using FoodsharingSiegen.Contracts.Entity;
using FoodsharingSiegen.Server.Auth;
using Microsoft.EntityFrameworkCore;
namespace FoodsharingSiegen.Server.Data.Service
{
///
/// The user service class (a. beging, 11.04.2022)
///
///
public class UserService : ServiceBase
{
private AuditService AuditService { get; }
#region Setup/Teardown
///
/// Initializes a new instance of the class
///
/// The context
///
///
public UserService(FsContext context, AuthService authService, AuditService auditService) : base(context, authService) => AuditService = auditService;
#endregion
#region Public Method AddUserAsync
///
/// Adds the user using the specified user (a. beging, 11.04.2022)
///
/// The user
/// A task containing an operation result of user
public async Task> AddUserAsync(User user)
{
try
{
if (await Context.Users!.AnyAsync(x => x.Mail.ToLower().Equals(user.Mail.ToLower())))
return new OperationResult(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(new Exception("Fehler bei der Registrierung"));
await AuditService.Insert(AuditType.CreateUser, user.Mail);
return new OperationResult(user);
}
catch (Exception e)
{
return new OperationResult(e);
}
}
#endregion
#region Public Method GetUsersAsync
////////////////////////////////////////////////////////////////////////////////////////////////////
/// Gets users asynchronous.
///
/// A Beging, 20.10.2021.
///
/// An asynchronous result that yields the users.
////////////////////////////////////////////////////////////////////////////////////////////////////
public async Task>> GetUsersAsync()
{
try
{
var users = await Context.Users!.AsNoTracking().ToListAsync();
return new OperationResult>(users);
}
catch (Exception e)
{
return new OperationResult>(e);
}
}
#endregion
#region Public Method SetPassword
///
/// Sets the password using the specified user (a. beging, 20.05.2022)
///
/// The user
/// A task containing the operation result
public async Task 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"));
var auditData = CurrentUser?.Id == user.Id ? "sich selbst" : user.Mail;
await AuditService.Insert(AuditType.SetUserPassword, auditData);
return new OperationResult();
}
catch (Exception e)
{
return new OperationResult(e);
}
}
#endregion
#region Public Method Update
///
/// Updates the user (a. beging, 11.04.2022)
///
/// The user
/// A task containing the operation result
public async Task 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"));
await AuditService.Insert(AuditType.UpdateUser, user.Mail);
return new OperationResult();
}
catch (Exception e)
{
return new OperationResult(e);
}
}
#endregion
}
}