Exemplo n.º 1
0
 public StudentsController(ASRContext context)
 {
     baseUrl     = "https://localhost:44317/ASRapi/";
     SlotStudent = new StudentSlotViewModel();
 }
Exemplo n.º 2
0
        public async Task <IActionResult> MakeBooking(StudentSlotViewModel stdSlot)
        {
            ViewBag.Message = "";
            var dateSlot  = stdSlot.slot.StartTime.Date;
            var timeSlot  = Request.Form["timeSlot"];
            var studentId = stdSlot.student.StudentID;

            Student student = await GetStudent(studentId);

            stdSlot.slot.StartTime = stdSlot.slot.StartTime + TimeSpan.Parse(timeSlot);
            var StartTime = stdSlot.slot.StartTime.ToString("dd/MM/yyyy HH:mm");

            ViewBag.id      = studentId + "@student.rmit.edu.au";
            ViewBag.Student = student.FirstName + " " + student.LastName;

            Slot bookedSlot = new Slot
            {
                RoomID    = stdSlot.slot.Room.RoomID,
                StartTime = stdSlot.slot.StartTime,
                StaffID   = stdSlot.slot.StaffID,
                StudentID = stdSlot.student.StudentID
            };

            if (bookedSlot.StudentID == "")
            {
                ModelState.AddModelError("", "StudentID still empty");
                return(View(stdSlot));
            }

            var allSlots = await GetAllSlots();

            //Check whether the student already make booking in the same day
            if (allSlots.Where(s => s.StudentID == bookedSlot.StudentID && s.StartTime.Date == bookedSlot.StartTime.Date).Any())
            {
                ViewBag.Message = "Student can only book 1 slot per day. Choose another day.";
                return(View());
            }
            else
            {
                if (allSlots.Where(s => s.RoomID == bookedSlot.RoomID && s.StartTime == bookedSlot.StartTime && s.StudentID != null).Any())
                {
                    ViewBag.Message = "Slot schedule not exist or already booked. Choose another slot.";
                    return(View());
                }
                else
                {
                    using (var client = new HttpClient())
                    {
                        //Parsing service base url
                        client.BaseAddress = new Uri(baseUrl);
                        client.DefaultRequestHeaders.Clear();

                        //Serialize the slot object to json file
                        StringContent content = new StringContent(JsonConvert.SerializeObject(bookedSlot), Encoding.UTF8, "application/json");

                        //Sending request to web api
                        HttpResponseMessage req = await client.PutAsync($"Slot?roomid={bookedSlot.RoomID}&startTime={StartTime}", content);

                        //Checking whether the request is successfull or not
                        if (req.IsSuccessStatusCode)
                        {
                            ViewBag.Message = "Slot has been booked!";
                            return(View());
                        }
                        else
                        {
                            ViewBag.Message = "Booking failed.";
                            return(View(stdSlot));
                        }
                    }
                }
            }
        }