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"));
interaction.ProspectId = Guid.Empty;
interaction.Created = DateTime.UtcNow;
targetProspect.Interactions.Add(interaction);
await Context.SaveChangesAsync();
// Detatch entities
Context.Entry(targetProspect).State = EntityState.Detached;
Context.Entry(interaction).State = EntityState.Detached;
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.AsNoTracking().Include(x => x.Interactions.OrderBy(i => i.Date)).ThenInclude(x => x.User).OrderBy(x => x.Name).ToListAsync();
return new OperationResult>(prospects);
}
catch (Exception e)
{
return new OperationResult>(e);
}
}
#endregion
#region Public Method RemoveInteraction
///
/// Removes the interaction using the specified interaction id (a. beging, 11.04.2022)
///
/// The interaction id
/// A task containing the operation result
public async Task RemoveInteraction(Guid interactionId)
{
try
{
Context.Interactions.Remove(new Interaction { Id = interactionId });
await Context.SaveChangesAsync();
return new OperationResult();
}
catch (Exception e)
{
return new OperationResult(e);
}
}
#endregion
#region Public Method UpdateAsync
///
/// Updates the prospect (a. beging, 11.04.2022)
///
/// The prospect
/// A task containing the operation result
public async Task UpdateAsync(Prospect prospect)
{
try
{
var entityProspect = await Context.Prospects.FirstOrDefaultAsync(x => x.Id == prospect.Id);
if (entityProspect == null) return new OperationResult(new Exception("Prospect not found"));
entityProspect.Memo = prospect.Memo;
entityProspect.Name = prospect.Name;
entityProspect.FsId = prospect.FsId;
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
}
}