using FoodsharingSiegen.Contracts;
using FoodsharingSiegen.Contracts.Entity;
using FoodsharingSiegen.Contracts.Enums;
using FoodsharingSiegen.Contracts.Model;
using FoodsharingSiegen.Server.Auth;
using Microsoft.EntityFrameworkCore;
namespace FoodsharingSiegen.Server.Data.Service
{
///
/// The prospect service class (a. beging, 01.04.2022)
///
///
public class ProspectService : ServiceBase
{
private AuditService AuditService { get; }
#region Setup/Teardown
///
/// Initializes a new instance of the class
///
/// The context
///
///
public ProspectService(FsContext context, AuthService authService, AuditService auditService) : base(context, authService) => AuditService = auditService;
#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(new Exception("Invalid prospect id"));
interaction.ProspectID = Guid.Empty;
interaction.Created = DateTime.UtcNow;
targetProspect.Interactions.Add(interaction);
targetProspect.Modified = DateTime.UtcNow;
await Context.SaveChangesAsync();
await AuditService.Insert(AuditType.AddInteraction, targetProspect.Name, interaction.Type.ToString());
// Detatch entities
Context.Entry(targetProspect).State = EntityState.Detached;
Context.Entry(interaction).State = EntityState.Detached;
return new(interaction);
}
catch (Exception e)
{
return new(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(new Exception("Cannot be empty"));
prospect.Created = DateTime.UtcNow;
prospect.Modified = DateTime.UtcNow;
prospect.Id = Guid.Empty;
await Context.Prospects!.AddAsync(prospect);
var saveR = await Context.SaveChangesAsync();
if (saveR > 0)
{
await AuditService.Insert(AuditType.CreateProspect, prospect.Name, prospect.FsId.ToString());
return new(prospect);
}
return new(new Exception("Couldn't add prospect"));
}
catch (Exception e)
{
return new(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(GetProspectsParameter parameter)
{
try
{
var prospectsQuery = Context.Prospects!.AsNoTracking().Include(x => x.Interactions.OrderBy(i => i.Date)).ThenInclude(x => x.User).OrderBy(x => x.Name).AsQueryable();
if(parameter.MustHaveInteractions != null && parameter.MustHaveInteractions.Any())
prospectsQuery = prospectsQuery.Where(x => x.Interactions.Any(i => parameter.MustHaveInteractions.Contains(i.Type)));
if(parameter.CannotHaveInteractions != null && parameter.CannotHaveInteractions.Any())
prospectsQuery = prospectsQuery.Where(x => x.Interactions.All(i => !parameter.CannotHaveInteractions.Contains(i.Type)));
if (!parameter.IncludeDeleted)
prospectsQuery = prospectsQuery.Where(x => x.RecordState != RecordState.Deleted);
var prospects = await prospectsQuery.ToListAsync();
return new(prospects);
}
catch (Exception e)
{
return new(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
{
var interaction = await Context.Interactions!.AsNoTracking().FirstOrDefaultAsync(x => x.Id == interactionId);
if(interaction == null) return new(new Exception("Interaction not found"));
Context.Interactions!.Remove(new() { Id = interaction.Id });
await Context.SaveChangesAsync();
// Update prospect modified date
var prospect = await Context.Prospects!.FirstOrDefaultAsync(x => x.Id == interaction.ProspectID);
if (prospect != null)
{
prospect.Modified = DateTime.UtcNow;
await Context.SaveChangesAsync();
}
await AuditService.Insert(AuditType.RemoveInteraction, "?");
return new();
}
catch (Exception e)
{
return new(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(new Exception("Prospect not found"));
entityProspect.Memo = prospect.Memo;
entityProspect.Name = prospect.Name;
entityProspect.FsId = prospect.FsId;
entityProspect.Warning = prospect.Warning;
entityProspect.RecordState = prospect.RecordState;
entityProspect.Modified = DateTime.UtcNow;
var saveR = await Context.SaveChangesAsync();
if(saveR < 1) return new(new Exception("Fehler beim speichern"));
await AuditService.Insert(AuditType.EditProspect, prospect.Name);
return new();
}
catch (Exception e)
{
return new(e);
}
}
#endregion
}
}