Ejemplo n.º 1
0
        public IHttpActionResult PutRoomReseravtion(int id, RoomReseravtion roomReseravtion)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != roomReseravtion.Room_Id)
            {
                return(BadRequest());
            }


            db.Entry(roomReseravtion).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!RoomReseravtionExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Ejemplo n.º 2
0
        public IHttpActionResult PostRoomReseravtion(RoomReseravtion roomReseravation)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var username = User.Identity.GetUserName();
            var user     = UserManager.FindByName(username);
            int userId   = user.appUserId;

            roomReseravation.AppUser_Id = userId;

            //TODO logika za provjeru
            roomReseravation.Timestamp = DateTime.Now;


            if (validateReservation(roomReseravation))
            {
                lock (locker)
                {
                    db.RoomReseravtions.Add(roomReseravation);

                    try
                    {
                        db.SaveChanges();
                    }
                    catch (DbUpdateException)
                    {
                        if (RoomReseravtionExists(roomReseravation.Room_Id))
                        {
                            return(Conflict());
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
            }
            else
            {
                var roomRes = this.db.RoomReseravtions.Where(x => x.Room_Id == roomReseravation.Room_Id).ToList();


                string msg = "Room is busy at: ";

                foreach (var roomRe in roomRes)
                {
                    msg += "  [" + roomRe.StartTime.Value.ToShortDateString() + "  -  " + roomRe.EndTime.Value.ToShortDateString() + "]";
                }

                return(Content(HttpStatusCode.BadRequest, msg));
            }



            return(CreatedAtRoute("ReservationApi", new { id = roomReseravation.Id }, roomReseravation));
        }
Ejemplo n.º 3
0
        public IHttpActionResult GetRoomReseravtion(int id)
        {
            RoomReseravtion roomReseravtion = db.RoomReseravtions.Find(id);

            if (roomReseravtion == null)
            {
                return(NotFound());
            }

            return(Ok(roomReseravtion));
        }
Ejemplo n.º 4
0
        public IHttpActionResult DeleteRoomReseravtion(int id)
        {
            RoomReseravtion roomReseravtion = db.RoomReseravtions.Find(id);

            if (roomReseravtion == null)
            {
                return(NotFound());
            }

            db.RoomReseravtions.Remove(roomReseravtion);
            db.SaveChanges();

            return(Ok(roomReseravtion));
        }
Ejemplo n.º 5
0
        public bool validateReservation(RoomReseravtion roomReservation)
        {
            if (roomReservation.StartTime > roomReservation.EndTime)
            {
                return(false);
            }
            List <RoomReseravtion> reservationList = db.RoomReseravtions
                                                     .Where(reservation => reservation.Room_Id == roomReservation.Room_Id)
                                                     .ToList();

            foreach (var reservation in reservationList)
            {
                if (Intersects(reservation, roomReservation))
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 6
0
        public bool Intersects(RoomReseravtion reservation1, RoomReseravtion reservation2)
        {
            if (reservation1.StartTime > reservation1.EndTime || reservation2.StartTime > reservation2.EndTime)
            {
                return(false);
            }

            if (reservation1.StartTime == reservation1.EndTime || reservation2.StartTime == reservation2.EndTime)
            {
                return(false); // No actual date range
            }
            if (reservation1.StartTime == reservation2.StartTime || reservation1.EndTime == reservation2.EndTime)
            {
                return(true); // If any set is the same time, then by default there must be some overlap.
            }
            if (reservation1.StartTime < reservation2.StartTime)
            {
                if (reservation1.EndTime > reservation2.StartTime && reservation1.EndTime < reservation2.EndTime)
                {
                    return(true); // Condition 1
                }
                if (reservation1.EndTime > reservation2.EndTime)
                {
                    return(true); // Condition 3
                }
            }
            else
            {
                if (reservation2.EndTime > reservation1.StartTime && reservation2.EndTime < reservation1.EndTime)
                {
                    return(true); // Condition 2
                }
                if (reservation2.EndTime > reservation1.EndTime)
                {
                    return(true); // Condition 4
                }
            }

            return(false);
        }