From 3f4c0720e1e3421431e7baa20882a4a4512a7fab Mon Sep 17 00:00:00 2001 From: ivar Date: Sun, 19 Oct 2025 23:41:23 +0200 Subject: Initial --- src/Services/AppReservationService.cs | 76 ++++++++++++++++++++++ src/Services/Interfaces/IAppReservationService.cs | 29 +++++++++ src/Services/Interfaces/IUserService.cs | 28 +++++++++ src/Services/UserService.cs | 77 +++++++++++++++++++++++ 4 files changed, 210 insertions(+) create mode 100644 src/Services/AppReservationService.cs create mode 100644 src/Services/Interfaces/IAppReservationService.cs create mode 100644 src/Services/Interfaces/IUserService.cs create mode 100644 src/Services/UserService.cs (limited to 'src/Services') diff --git a/src/Services/AppReservationService.cs b/src/Services/AppReservationService.cs new file mode 100644 index 0000000..2fc121b --- /dev/null +++ b/src/Services/AppReservationService.cs @@ -0,0 +1,76 @@ +using System; +using System.Linq; +using System.Text; +using IOL.Fagprove.Data; +using IOL.Fagprove.Data.Enums; +using IOL.Fagprove.Data.Models; +using IOL.Fagprove.Services.Interfaces; +using IOL.Fagprove.Utilities; + +namespace IOL.Fagprove.Services +{ + public class AppReservationService : IAppReservationService + { + private readonly AppDbContext _context; + + public AppReservationService(AppDbContext context) + { + _context = context; + } + + public bool ReservationIsPossible(Reservation reservation) + { + var reservationsForThisCabin = _context.Reservations.Where(r => + r.ReservationObjectId == reservation.ReservationObjectId + && r.Status == ReservationStatus.Granted + && DateTime.Compare(r.From, DateTime.Today) >= 0).ToList(); + if (reservationsForThisCabin.Count == 0) return true; + return reservationsForThisCabin.Select(item => item.From < reservation.To && reservation.From < item.To).All(overlaps => !overlaps); + } + + public bool ReservationIsValid(Reservation reservation) + { + if (reservation.From == default) return false; + if (reservation.To == default) return false; + if (reservation.UserId == default) return false; + if (reservation.ReservationObjectId == default) return false; + if (reservation.To < reservation.From) return false; + return reservation.From < reservation.To; + } + + public bool SendReservationStatusMail(Reservation reservation) + { + var user = _context.Users.SingleOrDefault(u => u.Id == reservation.UserId); + if (user == default) return false; + if (reservation.Status == ReservationStatus.Pending) return false; + var cabin = _context.Cabins.SingleOrDefault(c => c.Id == reservation.ReservationObjectId); + var message = new StringBuilder(); + var title = ""; + message.AppendLine("Hei " + user.Name); + message.AppendLine(); + switch (reservation.Status) + { + case ReservationStatus.Granted: + title = "Reservasjon innvilget"; + message.AppendLine("Din reservasjon er godkjent!"); + break; + case ReservationStatus.Rejected: + title = "Reservasjon avvist"; + message.AppendLine("Din reservasjon er avvist!"); + break; + } + message.AppendLine(); + message.AppendLine("Hytte: " + cabin?.Name); + message.AppendLine("Fra: " + reservation.From.ToString("dd-MM-yyyy")); + message.AppendLine("Til: " + reservation.To.ToString("dd-MM-yyyy")); + var statusMail = new Email + { + Message = message.ToString(), + Recepient = user.Email, + Title = title + " - Din bedrifts reservasjonstjeneste" + }; + statusMail.Send(); + return statusMail.Sent; + } + } +} \ No newline at end of file diff --git a/src/Services/Interfaces/IAppReservationService.cs b/src/Services/Interfaces/IAppReservationService.cs new file mode 100644 index 0000000..cf41480 --- /dev/null +++ b/src/Services/Interfaces/IAppReservationService.cs @@ -0,0 +1,29 @@ +using System; +using System.Threading.Tasks; +using IOL.Fagprove.Data.Models; + +namespace IOL.Fagprove.Services.Interfaces +{ + public interface IAppReservationService + { + /// + /// Check current reservations to determine if provided reservation is possible to make. + /// + /// Reservation to check. + /// Boolean indicating if the reservation is possible or not + bool ReservationIsPossible(Reservation reservation); + /// + /// Sends a status mail to the user that created provided reservation. + /// The contents of the message is determined by the provided reservation status. + /// + /// Reservation to send email on. + /// Boolean indicating if the status mail was sent. + bool SendReservationStatusMail(Reservation reservation); + /// + /// Determines if the provided reservation is valid and can be saved to db. + /// + /// Reservation to check. + /// Boolean indicating if the reservation is valid or not. + bool ReservationIsValid(Reservation reservation); + } +} \ No newline at end of file diff --git a/src/Services/Interfaces/IUserService.cs b/src/Services/Interfaces/IUserService.cs new file mode 100644 index 0000000..f315c24 --- /dev/null +++ b/src/Services/Interfaces/IUserService.cs @@ -0,0 +1,28 @@ +using IOL.Fagprove.Data.Models; +using IOL.Fagprove.Data.DTOs; + +namespace IOL.Fagprove.Services.Interfaces +{ + public interface IUserService + { + /// + /// Sends welcome mail to the provided user. + /// + /// User to send mail to. + /// Boolean indicating if the mail was sent or not. + bool SetTemporaryPasswordAndSendWelcomeMail(User user); + /// + /// Creates and sets a new temporary password on a user. + /// + /// User to work on. + /// Returns the password or default if the task was unsuccesful. + bool SetNewTemporaryPasswordAndNotifyUser(User user); + /// + /// Updates the provided users password with the provided password. + /// + /// User to update. + /// Unhashed new password. + /// Boolean indicating if the new password was set and saved to the database. + bool UpdatePassword(User user, string password); + } +} \ No newline at end of file diff --git a/src/Services/UserService.cs b/src/Services/UserService.cs new file mode 100644 index 0000000..6346f0b --- /dev/null +++ b/src/Services/UserService.cs @@ -0,0 +1,77 @@ +using System.Text; +using IOL.Fagprove.Data; +using IOL.Fagprove.Data.Models; +using IOL.Fagprove.Services.Interfaces; +using IOL.Fagprove.Utilities; + +namespace IOL.Fagprove.Services +{ + public class UserService : IUserService + { + private readonly AppDbContext _context; + + public UserService(AppDbContext context) + { + _context = context; + } + + public bool SetTemporaryPasswordAndSendWelcomeMail(User user) + { + var message = new StringBuilder(); + var temporaryPassword = SetNewTemporaryPassword(user); + message.AppendLine("Hei " + user.Name); + message.AppendLine("Du har fått en bruker i din bedrifts reservasjonstjeneste."); + message.AppendLine("Logg inn på ... med din e-postadresse og passord: " + temporaryPassword); + message.AppendLine("Vi oppfordrer deg til å skifte passord med en gang du har logget på."); + var welcomeEmail = new Email + { + Message = message.ToString(), + Recepient = user.Email, + Title = "Ny bruker - Din bedrifts reservasjonstjeneste" + }; + welcomeEmail.Send(); + return welcomeEmail.Sent; + } + + public bool SetNewTemporaryPasswordAndNotifyUser(User user) + { + var message = new StringBuilder(); + var temporaryPassword = SetNewTemporaryPassword(user); + message.AppendLine("Hei " + user.Name); + message.AppendLine("Du har fått ett midlertidig passord som du kan bruke til å sette nytt passord på din bruker."); + message.AppendLine("Logg inn på ... med din e-postadresse og passord: " + temporaryPassword); + message.AppendLine("Vi oppfordrer deg til å skifte passord med en gang du har logget på."); + var passwordEmail = new Email + { + Message = message.ToString(), + Recepient = user.Email, + Title = "Midlertidig passord - Din bedrifts reservasjonstjeneste" + }; + passwordEmail.Send(); + return passwordEmail.Sent; + } + + public bool UpdatePassword(User user, string password) + { + var passwordHash = PasswordHasher.HashPassword(password); + user.Password = passwordHash; + _context.Users.Update(user); + _context.Entry(user).Property(x => x.CreatedBy).IsModified = false; + _context.Entry(user).Property(x => x.CreatedUtc).IsModified = false; + _context.Entry(user).Property(x => x.Id).IsModified = false; + _context.SaveChanges(); + return true; + } + + private string SetNewTemporaryPassword(User user) + { + var temporaryPassword = Cryptography.RandomString(6); + var temporaryPasswordHash = PasswordHasher.HashPassword(temporaryPassword); + user.Password = temporaryPasswordHash; + _context.Users.Update(user); + _context.SaveChanges(); + return temporaryPassword; + } + } +} + -- cgit v1.3