diff --git a/FoodsharingSiegen.Server/Data/Service/ProspectService.cs b/FoodsharingSiegen.Server/Data/Service/ProspectService.cs new file mode 100644 index 0000000..140ceb3 --- /dev/null +++ b/FoodsharingSiegen.Server/Data/Service/ProspectService.cs @@ -0,0 +1,109 @@ +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 + } +} \ No newline at end of file diff --git a/FoodsharingSiegen.Server/Program.cs b/FoodsharingSiegen.Server/Program.cs index 39efe40..8639da5 100644 --- a/FoodsharingSiegen.Server/Program.cs +++ b/FoodsharingSiegen.Server/Program.cs @@ -17,6 +17,7 @@ builder.Services.AddDbContextFactory(opt => // DI builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddBlazorise(options => { options.ChangeTextOnKeyPress = true; }).AddMaterialProviders().AddMaterialIcons();