Users page and users service
This commit is contained in:
21
FoodsharingSiegen.Server/Data/Service/ServiceBase.cs
Normal file
21
FoodsharingSiegen.Server/Data/Service/ServiceBase.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
namespace FoodsharingSiegen.Server.Data.Service
|
||||
{
|
||||
public class ServiceBase
|
||||
{
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// <summary> Database Context </summary>
|
||||
///
|
||||
/// <value> The context. </value>
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
protected FsContext Context { get; }
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// <summary> Specialised constructor for use only by derived class. </summary>
|
||||
///
|
||||
/// <remarks> A Beging, 20.10.2021. </remarks>
|
||||
///
|
||||
/// <param name="context"> The context. </param>
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
protected ServiceBase(FsContext context) => Context = context;
|
||||
}
|
||||
}
|
||||
73
FoodsharingSiegen.Server/Data/Service/UserService.cs
Normal file
73
FoodsharingSiegen.Server/Data/Service/UserService.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
using FoodsharingSiegen.Contracts;
|
||||
using FoodsharingSiegen.Contracts.Entity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace FoodsharingSiegen.Server.Data.Service
|
||||
{
|
||||
public class UserService : ServiceBase
|
||||
{
|
||||
public UserService(FsContext context) : base(context)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public async Task<OperationResult> Update(User user)
|
||||
{
|
||||
try
|
||||
{
|
||||
Context.Users.Update(user);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// <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.ToListAsync();
|
||||
return new OperationResult<List<User>>(users);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return new OperationResult<List<User>>(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user