// GET: Slots/Book https://localhost:44300/Slots/Book?RoomID=A&StartTime=2019-02-20T10%3A00%3A00
        public IActionResult Book(string RoomID, String StartTime, String BookingID)
        {
            //Check the RoomID & StartTime fields are there
            if ((string.IsNullOrEmpty(RoomID)) && (string.IsNullOrEmpty(StartTime)))
            {
                return NotFound();
            }

            //Try get the DateTime from the input string StartTime
            if (!(DateTime.TryParse(StartTime, out DateTime startTimeValue)))
            {
                return NotFound();
            }

            //Find the selected slot from the repo/db
            var selectedSlot = _repo.Find(RoomID, startTimeValue);

            //if null then nothing was found
            if (selectedSlot == null)
            {
                return NotFound();
            }
            else
            {
                var validSlot = ValidateBookSlot(selectedSlot.RoomID, selectedSlot.StartTime, selectedSlot.StudentID);
                if (validSlot != null)
                {
                    _repo.Book(selectedSlot, BookingID);
                    return View("~/Views/Slots/SuccessfulBooking.cshtml");
                }
                
            }
            return NotFound();
        }
Beispiel #2
0
        public IActionResult Book(string studentID, [FromBody] Slot slot)
        {
            //Check if staffID is valid
            var selectedStudent = _db.Student.FirstOrDefault(s => s.StudentID == studentID.ToLower());

            if (selectedStudent == null)
            {
                return(NotFound(new { status = "fail", message = "No matching student ID found" }));
            }

            //Check if slot was submitted
            if (slot == null)
            {
                return(NotFound(new { status = "fail", message = "Slot information must be supplied" }));
            }

            //Check if slot exists
            var selectedSlot = repo.Find(slot.RoomID, slot.StartTime);

            if (selectedSlot == null)
            {
                return(NotFound(new { status = "fail", message = "Slot does not exist" }));
            }


            //Check if slot is already booked
            if (selectedSlot.StudentID != null)
            {
                return(NotFound(new { status = "fail", message = "Slot already has a booking" }));
            }


            if (ModelState.IsValid)
            {
                //Update the staff
                var bookedSlot = repo.Book(selectedSlot, studentID);

                //Check the staff was updated
                if (bookedSlot != null)
                {
                    return(Ok(new { status = "success", message = "Slot has been booked", data = new { slot = bookedSlot } }));
                }
            }
            return(NotFound(new { status = "fail", message = "Cannot book slot", data = ModelState.Values.Select(v => v.Errors) }));
        }