public void ValidateModel_ModelIsNull_ReturnFalse() { // Arrange var uow = new Mock <IUnitOfWork>(); var schedulerEventValidation = new SchedulerEventValidation(uow.Object); SchedulerEventModel model = null; // Act var result = schedulerEventValidation.IsValidSchedulerEventModel(model); // Assert Assert.IsFalse(result); }
public async Task <StatusCodeResult> Update([FromBody] SchedulerEventModel model) { try { var validator = new SchedulerEventValidation(_unitOfWork); if (!validator.IsValidSchedulerEventModel(model)) { return(new StatusCodeResult(StatusCodes.Status403Forbidden)); } var reservation = _unitOfWork.ReservationRepository.GetAll().Include(x => x.Room).FirstOrDefault(x => x.Id == model.Id); if (reservation == null) { return(new StatusCodeResult(StatusCodes.Status404NotFound)); } // cannot update other reservations, except the ones with ownership if (!validator.IsOwnedByUser(_userManager.GetUserId(User), reservation.UserId)) { return(new StatusCodeResult(StatusCodes.Status403Forbidden)); } reservation.ModifiedDate = DateTime.Now; reservation.IPAddress = Request.HttpContext.Connection.RemoteIpAddress.ToString(); reservation.Start = Convert.ToDateTime(model.StartDate); reservation.End = Convert.ToDateTime(model.EndDate); reservation.Title = model.Title; reservation.Description = model.Description; reservation.Status = ReservationStatusEnum.Modified.ToString(); reservation.RoomId = model.RoomId; _unitOfWork.ReservationRepository.Update(reservation); _unitOfWork.Save(); var message = $"Reservation titled: {reservation.Title} {Environment.NewLine} from {reservation.Start.ToString("dd/MMM/yy HH:mm")} to {reservation.End.ToString("dd/MMM/yy HH:mm")} {Environment.NewLine} on room {reservation.Room.Name} has changed !"; await _hubContext.Clients.All.SendAsync("ReservationChanged", message); return(new StatusCodeResult(StatusCodes.Status200OK)); } catch (Exception ex) { _logger.LogError(ex.Message, ex); return(new StatusCodeResult(StatusCodes.Status500InternalServerError)); } }
public static SchedulerEventModel ToSchedulerModel(this Reservation model) { if (model == null) { return(null); } var reservation = new SchedulerEventModel { Id = model.Id, Title = model.Title, Description = model.Description, Status = model.Status, StartDate = model.Start.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), EndDate = model.End.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), Color = model.Room.Color, RoomId = model.RoomId }; return(reservation); }
public StatusCodeResult Insert([FromBody] SchedulerEventModel model) { try { var validator = new SchedulerEventValidation(_unitOfWork); if (!validator.IsValidSchedulerEventModel(model)) { return(new StatusCodeResult(StatusCodes.Status500InternalServerError)); } var ipAddress = Request.HttpContext.Connection.RemoteIpAddress.ToString(); var user = _userManager.GetUserAsync(HttpContext.User); if (user != null && user.Result != null) { var reservation = new Reservation { Description = model.Description, Start = Convert.ToDateTime(model.StartDate), End = Convert.ToDateTime(model.EndDate), RoomId = model.RoomId, Status = model.Status, Title = model.Title, AddedDate = DateTime.Now, ModifiedDate = DateTime.Now, IPAddress = ipAddress, UserId = user.Result.Id }; _unitOfWork.ReservationRepository.Insert(reservation); _unitOfWork.Save(); } return(new StatusCodeResult(StatusCodes.Status201Created)); } catch (Exception ex) { _logger.LogError(ex.Message, ex); return(new StatusCodeResult(StatusCodes.Status500InternalServerError)); } }
public bool IsValidSchedulerEventModel(SchedulerEventModel model) { bool isValidModel = false; if (model == null) { return(isValidModel); } if (string.IsNullOrWhiteSpace(model.StartDate) || string.IsNullOrWhiteSpace(model.EndDate)) { return(isValidModel); } var modelStartDate = DateTime.Parse(model.StartDate); var modelEndDate = DateTime.Parse(model.EndDate); var reservationsByRoom = _unitOfWork.ReservationRepository .GetAll().Include(x => x.Room) .Where(r => r.RoomId == model.RoomId) .Where(r => r.Start.Day == modelStartDate.Day && r.End.Day == modelEndDate.Day) .Where(r => r.Id != model.Id); // we're not interrested in the current reservation if (modelStartDate > modelEndDate || modelEndDate < modelStartDate) { return(isValidModel); } foreach (var reservation in reservationsByRoom) { if (IsOverlapping(reservation, modelStartDate, modelEndDate)) { isValidModel = false; return(isValidModel); } } isValidModel = true; return(isValidModel); }
public static Reservation ToReservationModel(this SchedulerEventModel model) { if (model == null) { return(null); } var reservation = new Reservation { Id = model.Id, Title = model.Title, Description = model.Description, Status = model.Status, Start = Convert.ToDateTime(model.StartDate, CultureInfo.InvariantCulture), End = Convert.ToDateTime(model.EndDate, CultureInfo.InvariantCulture), UserId = model.UserId, RoomId = model.RoomId, IPAddress = model.IpAddress, AddedDate = DateTime.Today, ModifiedDate = DateTime.Today }; return(reservation); }