private void OnBeforeSaving()
        {
            var    entries = ChangeTracker.Entries();
            string userId;

            userId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
            if (string.IsNullOrWhiteSpace(userId))
            {
                userId = AppUsers.Where(u => u.Email == _rciConfig.Value.AdminEmailAddress).FirstOrDefault().Id;
            }

            var now = DateTime.Now;

            foreach (var entry in entries)
            {
                if (entry.Entity is IAuditable auditable)
                {
                    switch (entry.State)
                    {
                    case EntityState.Modified:
                        auditable.LastModified     = now;
                        auditable.LastModifiedById = userId;
                        break;

                    case EntityState.Added:
                        auditable.Created          = now;
                        auditable.LastModified     = now;
                        auditable.LastModifiedById = userId;
                        break;
                    }
                }
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> OnPostAsync(int reservationid, int roomid, int buildingid, int departmentid, string date)
        {
            CurrentUser = await _userManager.GetUserAsync(base.User);

            if (CurrentUser == null)
            {
                return(base.NotFound($"Unable to load user with ID '{_userManager.GetUserId(base.User)}'."));
            }

            BuildingIdRoute   = buildingid;
            DepartmentIdRoute = departmentid;

            DateTime.TryParse(date, out var ParseDate);
            Date = ParseDate;

            DateTime.TryParse(DateInputString, out var ParseStringDate);

            ReservationIdRoute = reservationid;



            AllNumbers = await _context.AppUsers
                         .Select(i => i.Number)
                         .ToListAsync();

            AppUsers = await _context.AppUsers
                       .AsNoTracking()
                       .ToListAsync();

            Reservations = await _context.Reservations
                           .Where(r => r.ReservationID != reservationid)
                           .Where(b => b.Room.BuildingID == buildingid)
                           .Where(d => d.Date == ParseStringDate)
                           .Where(r => r.RoomID == Reservation.RoomID)
                           .AsNoTracking()
                           .ToListAsync();

            Reservation = await _context.Reservations
                          .Include(r => r.ApplicationUser)
                          .Include(r => r.Room)
                          .ThenInclude(r => r.Building)
                          .ThenInclude(r => r.Department)
                          .AsNoTracking()
                          .SingleOrDefaultAsync(m => m.ReservationID == reservationid);

            Rooms = await _context.Rooms
                    .Where(b => b.BuildingID == Reservation.Room.BuildingID)
                    .AsNoTracking().OrderBy(r => r.Number)
                    .ToListAsync();

            Hours = new List <String>
            {
                new DateTime(2018, 1, 1, 8, 00, 0).ToShortTimeString(),
                new DateTime(2018, 1, 1, 8, 30, 0).ToShortTimeString(),
                new DateTime(2018, 1, 1, 9, 00, 0).ToShortTimeString(),
                new DateTime(2018, 1, 1, 9, 30, 0).ToShortTimeString(),
                new DateTime(2018, 1, 1, 10, 00, 0).ToShortTimeString(),
                new DateTime(2018, 1, 1, 10, 30, 0).ToShortTimeString(),
                new DateTime(2018, 1, 1, 11, 00, 0).ToShortTimeString(),
                new DateTime(2018, 1, 1, 11, 30, 0).ToShortTimeString(),
                new DateTime(2018, 1, 1, 12, 00, 0).ToShortTimeString(),
                new DateTime(2018, 1, 1, 12, 30, 0).ToShortTimeString(),
                new DateTime(2018, 1, 1, 13, 00, 0).ToShortTimeString(),
                new DateTime(2018, 1, 1, 13, 30, 0).ToShortTimeString(),
                new DateTime(2018, 1, 1, 14, 00, 0).ToShortTimeString(),
                new DateTime(2018, 1, 1, 14, 30, 0).ToShortTimeString(),
                new DateTime(2018, 1, 1, 15, 00, 0).ToShortTimeString(),
                new DateTime(2018, 1, 1, 15, 30, 0).ToShortTimeString(),
                new DateTime(2018, 1, 1, 16, 00, 0).ToShortTimeString(),
                new DateTime(2018, 1, 1, 16, 30, 0).ToShortTimeString(),
                new DateTime(2018, 1, 1, 17, 00, 0).ToShortTimeString(),
                new DateTime(2018, 1, 1, 17, 30, 0).ToShortTimeString(),
                new DateTime(2018, 1, 1, 18, 00, 0).ToShortTimeString(),
                new DateTime(2018, 1, 1, 18, 30, 0).ToShortTimeString(),
                new DateTime(2018, 1, 1, 19, 00, 0).ToShortTimeString(),
                new DateTime(2018, 1, 1, 19, 30, 0).ToShortTimeString(),
                new DateTime(2018, 1, 1, 20, 00, 0).ToShortTimeString()
            };



            ViewData["StartTime"] = new SelectList(Hours, Reservation.StartTime.ToShortTimeString());
            ViewData["EndTime"]   = new SelectList(Hours, Reservation.EndTime.ToShortTimeString());


            // lista, atrybut ustawiany, atrybut wyświetlany, domyślnie wybrana pozycja z listy
            ViewData["Rooms"] = new SelectList(Rooms, "RoomID", "Number", Reservation.RoomID);

            if (StartTime == EndTime)
            {
                ErrorString = "Rezerwacja musi trwać co najmniej 30 minut.";
            }
            if (StartTime > EndTime)
            {
                ErrorString = "Rezerwacja nie może się zacząć później niż skończyć.";
            }

            foreach (var item in Reservations)
            {
                if (StartTime == item.StartTime || EndTime == item.EndTime)
                {
                    ErrorString = "W tym czasie trwa inna rezerwacja.";
                }
                if (EndTime < item.EndTime && StartTime < item.StartTime && Reservation.EndTime > item.StartTime)
                {
                    ErrorString = "W tym czasie trwa inna rezerwacja.";
                }
                if (EndTime > item.EndTime && StartTime < item.StartTime)
                {
                    ErrorString = "W tym czasie trwa inna rezerwacja.";
                }
                if (EndTime > item.EndTime && StartTime > item.StartTime && Reservation.StartTime < item.EndTime)
                {
                    ErrorString = "W tym czasie trwa inna rezerwacja.";
                }
                if (EndTime < item.EndTime && StartTime > item.StartTime)
                {
                    ErrorString = "W tym czasie trwa inna rezerwacja.";
                }
            }


            if (ErrorString != null)
            {
                return(Page());
            }



            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var reservationToUpdate = await _context.Reservations
                                      .SingleOrDefaultAsync(m => m.ReservationID == reservationid);

            var currentUserRoles = await _userManager.GetRolesAsync(CurrentUser);

            if (currentUserRoles.First() == "administrator")
            {
                if (!AllNumbers.Contains(Number))
                {
                    NumberError = String.Format("Nie ma takiego człeka o numerze: {0}", Number);
                    return(Page());
                }
                reservationToUpdate.Id = AppUsers.Where(i => i.Number == Number).Select(i => i.Id).FirstOrDefault();
            }
            if (currentUserRoles.First() == "użytkownik")
            {
                reservationToUpdate.Id = CurrentUser.Id;
            }

            reservationToUpdate.RoomID    = RoomID;
            reservationToUpdate.StartTime = StartTime;
            reservationToUpdate.EndTime   = EndTime;

            reservationToUpdate.Date = ParseStringDate;
            if (await TryUpdateModelAsync <Reservation>(
                    reservationToUpdate,
                    "Reservation",
                    s => s.RoomID, s => s.Note))
            {
                await _context.SaveChangesAsync();
            }

            return(Redirect("./Details" + "?" + "reservationid=" + reservationToUpdate.ReservationID.ToString() + "&buildingid=" + BuildingIdRoute.ToString() + "&departmentid=" + DepartmentIdRoute.ToString() + "&date=" + Date.ToShortDateString()));
        }
Esempio n. 3
0
        public async Task <IActionResult> OnPostAsync(int roomid, int buildingid, int departmentid, string date)
        {
            CurrentUser = await _userManager.GetUserAsync(base.User);

            if (CurrentUser == null)
            {
                return(base.NotFound($"Unable to load user with ID '{_userManager.GetUserId(base.User)}'."));
            }
            BuildingIdRoute   = buildingid;
            DepartmentIdRoute = departmentid;

            DateTime.TryParse(date, out var ParseDate);
            Date = ParseDate;

            AllNumbers = await _context.AppUsers
                         .Select(i => i.Number)
                         .ToListAsync();

            RoomId = roomid;



            Reservations = await _context.Reservations
                           .Where(b => b.Room.BuildingID == buildingid)
                           .Where(d => d.Date == Date)
                           .Where(r => r.RoomID == Reservation.RoomID)
                           .AsNoTracking()
                           .ToListAsync();

            Room = await _context.Rooms
                   .Where(i => i.RoomID == roomid)
                   .Include(b => b.Building)
                   .ThenInclude(d => d.Department)
                   .AsNoTracking()
                   .FirstOrDefaultAsync();

            AppUsers = await _context.AppUsers
                       .AsNoTracking()
                       .ToListAsync();


            if (!ModelState.IsValid)
            {
                return(Page());
            }

            Hours = new List <String>();
            Hours.Add(new DateTime(2018, 1, 1, 8, 00, 0).ToShortTimeString());
            Hours.Add(new DateTime(2018, 1, 1, 8, 30, 0).ToShortTimeString());
            Hours.Add(new DateTime(2018, 1, 1, 9, 00, 0).ToShortTimeString());
            Hours.Add(new DateTime(2018, 1, 1, 9, 30, 0).ToShortTimeString());
            Hours.Add(new DateTime(2018, 1, 1, 10, 00, 0).ToShortTimeString());
            Hours.Add(new DateTime(2018, 1, 1, 10, 30, 0).ToShortTimeString());
            Hours.Add(new DateTime(2018, 1, 1, 11, 00, 0).ToShortTimeString());
            Hours.Add(new DateTime(2018, 1, 1, 11, 30, 0).ToShortTimeString());
            Hours.Add(new DateTime(2018, 1, 1, 12, 00, 0).ToShortTimeString());
            Hours.Add(new DateTime(2018, 1, 1, 12, 30, 0).ToShortTimeString());
            Hours.Add(new DateTime(2018, 1, 1, 13, 00, 0).ToShortTimeString());
            Hours.Add(new DateTime(2018, 1, 1, 13, 30, 0).ToShortTimeString());
            Hours.Add(new DateTime(2018, 1, 1, 14, 00, 0).ToShortTimeString());
            Hours.Add(new DateTime(2018, 1, 1, 14, 30, 0).ToShortTimeString());
            Hours.Add(new DateTime(2018, 1, 1, 15, 00, 0).ToShortTimeString());
            Hours.Add(new DateTime(2018, 1, 1, 15, 30, 0).ToShortTimeString());
            Hours.Add(new DateTime(2018, 1, 1, 16, 00, 0).ToShortTimeString());
            Hours.Add(new DateTime(2018, 1, 1, 16, 30, 0).ToShortTimeString());
            Hours.Add(new DateTime(2018, 1, 1, 17, 00, 0).ToShortTimeString());
            Hours.Add(new DateTime(2018, 1, 1, 17, 30, 0).ToShortTimeString());
            Hours.Add(new DateTime(2018, 1, 1, 18, 00, 0).ToShortTimeString());
            Hours.Add(new DateTime(2018, 1, 1, 18, 30, 0).ToShortTimeString());
            Hours.Add(new DateTime(2018, 1, 1, 19, 00, 0).ToShortTimeString());
            Hours.Add(new DateTime(2018, 1, 1, 19, 30, 0).ToShortTimeString());
            Hours.Add(new DateTime(2018, 1, 1, 20, 00, 0).ToShortTimeString());

            ViewData["StartTime"] = new SelectList(Hours, StartTime.ToShortTimeString());
            ViewData["EndTime"]   = new SelectList(Hours, StartTime.AddMinutes(30).ToShortTimeString());

            if (Reservation.StartTime == Reservation.EndTime)
            {
                ErrorString = "Rezerwacja musi trwać co najmniej 30 minut.";
            }
            if (Reservation.StartTime > Reservation.EndTime)
            {
                ErrorString = "Rezerwacja nie może się zacząć później niż skończyć.";
            }

            foreach (var item in Reservations)
            {
                if (Reservation.StartTime == item.StartTime || Reservation.EndTime == item.EndTime)
                {
                    ErrorString = "W tym czasie trwa inna rezerwacja.";
                }
                if (Reservation.EndTime < item.EndTime && Reservation.StartTime < item.StartTime && Reservation.EndTime > item.StartTime)
                {
                    ErrorString = "W tym czasie trwa inna rezerwacja.";
                }
                if (Reservation.EndTime > item.EndTime && Reservation.StartTime < item.StartTime)
                {
                    ErrorString = "W tym czasie trwa inna rezerwacja.";
                }
                if (Reservation.EndTime > item.EndTime && Reservation.StartTime > item.StartTime && Reservation.StartTime < item.EndTime)
                {
                    ErrorString = "W tym czasie trwa inna rezerwacja.";
                }
                if (Reservation.EndTime < item.EndTime && Reservation.StartTime > item.StartTime)
                {
                    ErrorString = "W tym czasie trwa inna rezerwacja.";
                }
            }

            if (ErrorString != null)
            {
                return(Page());
            }


            var newReservation = new Reservation();

            DateTime.TryParse(DateInputString, out var ParseStringDate);
            newReservation.Date = ParseStringDate;

            var currentUserRoles = await _userManager.GetRolesAsync(CurrentUser);

            if (currentUserRoles.First() == "administrator")
            {
                if (!AllNumbers.Contains(Number))
                {
                    NumberError = String.Format("Nie ma takiego człeka o numerze: {0}", Number);
                    return(Page());
                }
                newReservation.Id = AppUsers.Where(i => i.Number == Number).Select(i => i.Id).FirstOrDefault();
            }
            if (currentUserRoles.First() == "użytkownik")
            {
                newReservation.Id = CurrentUser.Id;
            }


            if (await TryUpdateModelAsync <Reservation>(
                    newReservation,
                    "Reservation",
                    s => s.Note, s => s.StartTime, s => s.EndTime, s => s.RoomID))
            {
                _context.Reservations.Add(newReservation);
                await _context.SaveChangesAsync();
            }

            return(Redirect("./Details" + "?" + "reservationid=" + newReservation.ReservationID.ToString() + "&buildingid=" + BuildingIdRoute.ToString() + "&departmentid=" + DepartmentIdRoute.ToString() + "&date=" + Date.ToShortDateString()));
        }