public HttpResponseMessage Delete([FromUri] int id) { var message = new HttpResponseMessage(); var bookingRepo = new Repositories.BookingRepository(_db); var concBookingRepo = new Repositories.ConcreteBookingRepository(_db); var concBook = concBookingRepo.GetById(id); var book = bookingRepo.GetById(concBook.BookingId); if (book != null) { //using (var transaction = new TransactionScope()) //{ bookingRepo.DeleteOnSubmit(book); _db.SafeSubmitChanges(); if (bookingRepo.Single(b => b == book) == null) message.OK(RESPONSE_OK); else message.Forbidden("You done derped, son"); //} } else message.Forbidden("Den valgte række i tabellen kunne ikke findes"); 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; }