ProspectService
This commit is contained in:
109
FoodsharingSiegen.Server/Data/Service/ProspectService.cs
Normal file
109
FoodsharingSiegen.Server/Data/Service/ProspectService.cs
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
using FoodsharingSiegen.Contracts;
|
||||||
|
using FoodsharingSiegen.Contracts.Entity;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace FoodsharingSiegen.Server.Data.Service
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The prospect service class (a. beging, 01.04.2022)
|
||||||
|
/// </summary>
|
||||||
|
/// <seealso cref="ServiceBase"/>
|
||||||
|
public class ProspectService : ServiceBase
|
||||||
|
{
|
||||||
|
#region Setup/Teardown
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="ProspectService"/> class
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="context">The context</param>
|
||||||
|
public ProspectService(FsContext context) : base(context) { }
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Public Method AddInteraction
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds the interaction using the specified interaction (a. beging, 02.04.2022)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="interaction">The interaction</param>
|
||||||
|
/// <returns>A task containing an operation result of interaction</returns>
|
||||||
|
public async Task<OperationResult<Interaction>> AddInteraction(Interaction interaction)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var targetProspect = await Context.Prospects.Include(x => x.Interactions).FirstOrDefaultAsync(x => x.Id == interaction.ProspectId);
|
||||||
|
if (targetProspect == null) return new OperationResult<Interaction>(new Exception("Invalid prospect id"));
|
||||||
|
|
||||||
|
var dummyUser = Context.Users.First().Id;
|
||||||
|
|
||||||
|
interaction.ProspectId = Guid.Empty;
|
||||||
|
interaction.UserId = dummyUser;
|
||||||
|
|
||||||
|
targetProspect.Interactions.Add(interaction);
|
||||||
|
|
||||||
|
await Context.SaveChangesAsync();
|
||||||
|
|
||||||
|
return new OperationResult<Interaction>(interaction);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
return new OperationResult<Interaction>(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Public Method AddProspectAsync
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds the prospect using the specified prospect (a. beging, 01.04.2022)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="prospect">The prospect</param>
|
||||||
|
/// <returns>A task containing an operation result of prospect</returns>
|
||||||
|
public async Task<OperationResult<Prospect>> AddProspectAsync(Prospect? prospect)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (prospect == null) return new OperationResult<Prospect>(new Exception("Cannot be empty"));
|
||||||
|
|
||||||
|
prospect.Created = DateTime.UtcNow;
|
||||||
|
prospect.Id = Guid.Empty;
|
||||||
|
|
||||||
|
await Context.Prospects.AddAsync(prospect);
|
||||||
|
var saveR = await Context.SaveChangesAsync();
|
||||||
|
|
||||||
|
if (saveR > 0)
|
||||||
|
return new OperationResult<Prospect>(prospect);
|
||||||
|
|
||||||
|
return new OperationResult<Prospect>(new Exception("Couldn't add prospect"));
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
return new OperationResult<Prospect>(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Public Method GetProspectsAsync
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the users (a. beging, 01.04.2022)
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>A task containing an operation result of list prospect</returns>
|
||||||
|
public async Task<OperationResult<List<Prospect>>> GetProspectsAsync()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var prospects = await Context.Prospects.Include(x => x.Interactions).ThenInclude(x => x.User).OrderBy(x => x.Name).ToListAsync();
|
||||||
|
return new OperationResult<List<Prospect>>(prospects);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
return new OperationResult<List<Prospect>>(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,6 +17,7 @@ builder.Services.AddDbContextFactory<FsContext>(opt =>
|
|||||||
// DI
|
// DI
|
||||||
builder.Services.AddScoped<FsContext>();
|
builder.Services.AddScoped<FsContext>();
|
||||||
builder.Services.AddScoped<UserService>();
|
builder.Services.AddScoped<UserService>();
|
||||||
|
builder.Services.AddScoped<ProspectService>();
|
||||||
|
|
||||||
builder.Services.AddBlazorise(options => { options.ChangeTextOnKeyPress = true; }).AddMaterialProviders().AddMaterialIcons();
|
builder.Services.AddBlazorise(options => { options.ChangeTextOnKeyPress = true; }).AddMaterialProviders().AddMaterialIcons();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user