public HttpResponseMessage Get() { var response = new HttpResponseMessage(); var bookings = new Repositories.PossibleBookingRepository(_db).GetAll(); response.OK(bookings.SerializeToJsonObject()); return response; }
public HttpResponseMessage Post([FromBody] Models.ConcreteBooking concreteBooking) { // Set the number of Errors to 0 var numberOfErrors = 0; // make new repository of the possible booking database table var possibleBookingRepo = new Repositories.PossibleBookingRepository(_db); // find the first possible booking that mathces the id in the concrete booking var possibleBooking = possibleBookingRepo.Single(pb => pb.Id == concreteBooking.PossibleBookingId); if (possibleBooking != null) { //Get subject from database if subject id exists var cls = _db.Subjects.FirstOrDefault(f => f.Id == possibleBooking.Subject.Id); if (cls != null) { // create a student repository based on the student database table var studentRepo = new Repositories.StudentRepository(_db); // correct the subject field in the object concreteBooking.Subject = _db.GetTable<Models.Subject>().FirstOrDefault(s => s.Name.Equals(concreteBooking.Subject.Name)); // correct the student field in the object concreteBooking.Student = studentRepo.Single(s => s.Username.Equals(concreteBooking.Student.Username)); // insert the object to the table in the database _db.GetTable<Models.ConcreteBooking>().InsertOnSubmit(concreteBooking); // write the changes _db.SafeSubmitChanges(ref numberOfErrors); } } else numberOfErrors++; //Create new HttpResponseMessage var message = new HttpResponseMessage(); //If there are errors send forbidden else send OK if (numberOfErrors != 0) message.Forbidden("Errors: " + numberOfErrors); else message.OK(RESPONSE_OK); //Returns the HttpResponseMessage return message; }
public HttpResponseMessage Put([FromUri]int id, [FromBody]Messages.PossibleBookingDelay delay) { var successResponse = "{\"Response\":\"Success\"}"; var message = new HttpResponseMessage(); var bookings = new Repositories.BookingRepository(_db).GetAll(); Models.PossibleBooking posBook = null; List<Models.ConcreteBooking> conBooks = null; Models.Booking bookPos = null; List<Models.Booking> bookCons = null; message = HttpResponse.Try<NullReferenceException>( action: () => { posBook = new Repositories .PossibleBookingRepository(_db) .GetById(id); if (posBook == null) throw new NullReferenceException("delay.Id did not match any stored IDs"); }, success: successResponse, failure: "Kunne ikke finde det korrekte id"); if (posBook != null) { Debug.WriteLine("posBook is not null"); bookPos = bookings.Single(book => book.Id == posBook.BookingId); message = HttpResponse.Try<Exception>( action: () => { conBooks = new Repositories .ConcreteBookingRepository(_db) .Where(conc => conc.PossibleBookingId == posBook.Id) .ToList(); if (conBooks == null || conBooks.Count == 0) throw new Exception("posBook.Id did not match any elements in the list"); }, success: successResponse, failure: "Kunne ikke finde konkrete bookinger"); if (conBooks != null && conBooks.Count > 0) { Debug.WriteLine("conBooks is not null, and contains " + conBooks.Count + " items"); bool access = true; message = HttpResponse.Try<Exception>( action: () => { bookCons = new List<Models.Booking>(); conBooks.ForEach(e => bookCons.Add(bookings.Single(book => book.Id == e.BookingId))); Debug.WriteLine("conBooks length: " + conBooks.Count); Debug.WriteLine("bookCons length: " + bookCons.Count); if (bookCons.Count > conBooks.Count) { access = false; throw new Exception("You have more bookings representing concrete bookings, than concrete bookings"); } else if (bookCons.Count < conBooks.Count) { access = false; throw new Exception("You have less bookings representing concrete bookings, than concrete bookings"); } }, success: successResponse, failure: "kunne ikke finde bookinger der matcher booking ID'et"); if (access) { Debug.WriteLine("Access granted"); bookPos.StartTime = bookPos.StartTime.Add(delay.Duration); foreach (var e in bookCons) { e.StartTime = e.StartTime.Add(delay.Duration); Debug.WriteLine("Start time: " + e.StartTime.ToString()); e.EndTime = e.EndTime.Add(delay.Duration); Debug.WriteLine("End time: " + e.EndTime.ToString()); } _db.SafeSubmitChanges(); Debug.WriteLine("Changes submitted"); } else Debug.WriteLine("Access denied"); } } return message; }