using FoodsharingSiegen.Contracts;
using FoodsharingSiegen.Contracts.Entity;
using Microsoft.EntityFrameworkCore;
namespace FoodsharingSiegen.Server.Data.Service
{
///
/// The prospect service class (a. beging, 01.04.2022)
///
///
public class ProspectService : ServiceBase
{
#region Setup/Teardown
///
/// Initializes a new instance of the class
///
/// The context
public ProspectService(FsContext context) : base(context) { }
#endregion
#region Public Method AddInteraction
///
/// Adds the interaction using the specified interaction (a. beging, 02.04.2022)
///
/// The interaction
/// A task containing an operation result of interaction
public async Task> 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(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);
}
catch (Exception e)
{
return new OperationResult(e);
}
}
#endregion
#region Public Method AddProspectAsync
///
/// Adds the prospect using the specified prospect (a. beging, 01.04.2022)
///
/// The prospect
/// A task containing an operation result of prospect
public async Task> AddProspectAsync(Prospect? prospect)
{
try
{
if (prospect == null) return new OperationResult(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);
return new OperationResult(new Exception("Couldn't add prospect"));
}
catch (Exception e)
{
return new OperationResult(e);
}
}
#endregion
#region Public Method GetProspectsAsync
///
/// Gets the users (a. beging, 01.04.2022)
///
/// A task containing an operation result of list prospect
public async Task>> GetProspectsAsync()
{
try
{
var prospects = await Context.Prospects.Include(x => x.Interactions).ThenInclude(x => x.User).OrderBy(x => x.Name).ToListAsync();
return new OperationResult>(prospects);
}
catch (Exception e)
{
return new OperationResult>(e);
}
}
#endregion
}
}