예제 #1
0
        public bool UpdateReservationEquipmentTable(int reservationId, int studioId, Dictionary <string, bool> equipments)
        {
            foreach (var eq in equipments)
            {
                if (eq.Value == true) //ha most kibéreltük az eszközt, de eddig nem volt
                {
                    //ha benne van már az adatbázisban, akkor nem kell csinálni semmit
                    //if (ReservationEquipmentPairs.FirstOrDefault(l => l.ReservationId == reservationId && l.EquipmentName == eq.Key) != null) { }
                    //ha még nincs benne az adatbázisban, akkor hozzá kell adni
                    if (ReservationEquipmentPairs.FirstOrDefault(l => l.ReservationId == reservationId && l.EquipmentName == eq.Key && l.StudioId == studioId) == null)
                    {
                        ReservationEquipmentPair temp = new ReservationEquipmentPair
                        {
                            ReservationId = reservationId,
                            StudioId      = studioId,
                            EquipmentId   = _context.Equipments.FirstOrDefault(l => l.Name == eq.Key && l.StudioId == studioId).Id,
                            EquipmentName = eq.Key
                        };
                        _context.ReservationEquipmentPairs.Add(temp);
                    }
                }
                else //ha eddig ki volt bérelve az eszköz, de most lemondtuk VAGY eddig SEM volt bérelve
                {
                    //ha eddig sem volt kibérelve, akkor nem kell csinálni semmit
                    //if (ReservationEquipmentPairs.FirstOrDefault(l => l.ReservationId == reservationId && l.EquipmentName == eq.Key && l.StudioId == studioId) == null) { }
                    //ha eddig ki volt bérelve, és lemondtuk
                    if (ReservationEquipmentPairs.FirstOrDefault(l => l.ReservationId == reservationId && l.EquipmentName == eq.Key && l.StudioId == studioId) != null)
                    {
                        ReservationEquipmentPair temp = ReservationEquipmentPairs.FirstOrDefault(l =>
                                                                                                 l.ReservationId == reservationId &&
                                                                                                 l.StudioId == studioId &&
                                                                                                 l.EquipmentName == eq.Key);
                        _context.ReservationEquipmentPairs.Remove(temp);
                    }
                }
            }

            try
            {
                _context.SaveChanges();
            }
            catch (Exception)
            {
                return(false);
            }

            return(true);
        }
예제 #2
0
        public async Task <bool> SaveReservationAsync(int?roomId, string userName, ReservationViewModel reservation) //ez csak a foglalás létrehozásánál hívódik meg
        {
            /*
             * ide hozzá kell majd írni hogy a reservationEqipmentPairs táblába bekerüljön a megfelelő elem
             */
            if (roomId == null || reservation == null)
            {
                return(false);
            }

            if (!Validator.TryValidateObject(reservation, new ValidationContext(reservation, null, null), null))
            {
                return(false);
            }

            DateTime start = reservation.Day.AddHours(reservation.StartHour);
            DateTime end   = reservation.Day.AddHours(reservation.EndHour);

            if (_reservationDateValidator.Validate(start, end, roomId.Value, "create", reservation.Id, reservation.Equipments) != ReservationDateError.None)
            {
                return(false);
            }

            User user = await _userManager.FindByNameAsync(userName);

            if (user == null)
            {
                return(false);
            }

            /*
             * List<Equipment> equipments = new List<Equipment>();
             *
             * foreach (var equipment in reservation.Equipments)
             * {
             *  if (equipment.Value)
             *  {
             *      Equipment e = _context.Equipments.FirstOrDefault(l => l.Name == equipment.Key);
             *      equipments.Add(e);
             *  }
             * }
             */
            _context.Reservations.Add(new Reservation
            {
                RehearsalRoomId = reservation.Room.Id,
                UserId          = user.Id,
                Start           = reservation.Day.AddHours(reservation.StartHour),
                End             = reservation.Day.AddHours(reservation.EndHour),
                BandName        = reservation.BandName
                                  //Equipments = equipments
                                  //remove equipments
            });

            try
            {
                _context.SaveChanges();
            }
            catch (Exception)
            {
                return(false);
            }

            int studioId = GetStudioByRoomId(reservation.Room.Id).Id;

            List <ReservationEquipmentPair> pairs = new List <ReservationEquipmentPair>();

            foreach (var eq in reservation.Equipments)
            {
                if (eq.Value == true) //ha ezt kibéreltük
                {
                    ReservationEquipmentPair pair = new ReservationEquipmentPair
                    {
                        StudioId      = studioId,
                        EquipmentId   = _context.Equipments.Where(l => l.StudioId == studioId).FirstOrDefault(l => l.Name == eq.Key).Id,
                        EquipmentName = eq.Key,
                        ReservationId = _context.Reservations.FirstOrDefault(l =>
                                                                             l.RehearsalRoomId == reservation.Room.Id &&
                                                                             l.UserId == user.Id &&
                                                                             l.BandName == reservation.BandName &&
                                                                             l.Start == reservation.Day.AddHours(reservation.StartHour) &&
                                                                             l.End == reservation.Day.AddHours(reservation.EndHour)).Id //somehow we have to find the last(?) added reservation
                    };
                }
            }

            foreach (var pair in pairs)
            {
                _context.ReservationEquipmentPairs.Add(pair);
            }

            try
            {
                _context.SaveChanges();
            }
            catch (Exception)
            {
                return(false);
            }

            return(true);
        }