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 Get([FromUri] string username, [FromUri] string password)
        {
            Bruger br = _db.Brugers.FirstOrDefault(b => b.Brugernavn == username && b.Password == password);

            var accessLevel = default(Models.AccessLevel);

            var response = new HttpResponseMessage();

            if (br != null)
            {
                // gets the access level depending on which table the user exists in
                accessLevel =
                    _db.Administrators.FirstOrDefault(a => a.Bruger_id == br._id) != null ? Models.AccessLevel.Administrator :
                    _db.Lærers.FirstOrDefault(l => l.Bruger_id == br._id) != null ? Models.AccessLevel.Teacher :
                    _db.Studerendes.FirstOrDefault(s => s.Bruger_id == br._id) != null ? Models.AccessLevel.Student :
                    Models.AccessLevel.Error;

                var auth = new Models.Authentification(username, password, accessLevel);
                _authRepo.Add(auth);

                Debug.WriteLine(string.Format(
                    "User logged in with credentials \"{0}\" and \"{1}\" at {2}", auth.Username, auth.Password, DateTime.Now));

                response.OK(auth.SerializeToJsonObject());
            }
            else
                response.Forbidden("Brugernavn eller adgangskode ikke korrekt");

            return response;
        }
        public HttpResponseMessage Get([FromUri]int teacherId)
        {
            var subjects = _db.Subjects;
            var response = new HttpResponseMessage();

            var subject = subjects.FirstOrDefault(s => s.Teacher.Id == teacherId);

            if (subject != null)
                response.OK(subject.SerializeToJsonObject());
            else
                response.Forbidden("Den pågældende lærer blev ikke fundet");

            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 Post(
            //[FromUri] string firstname, [FromUri] string lastname, [FromUri] string Username, [FromUri] string password, [FromUri] string homeroomClass)
            [FromBody]Models.Student student)
        {
            var numberOfErrors = 0;

            //var json = jsonBytes.ConvertToString();

            //var student = json.DeserializeJson<Models.Student>();

            Action<BookingSystemDataContext> submitChanges = db =>
            {
                try
                {
                    db.SubmitChanges();
                }
                catch (Exception e)
                {
                    numberOfErrors++;
                    Debug.WriteLine("DEBUG: " + e.Message);
                }
            };

            var classId = 0;
            var cls = _db.Holds.FirstOrDefault(h => h.Navn.Equals(student.HomeRoomClass.Name));

            // checks if cls exits in table
            if (cls != null)
            {
                // checks if it is an eal mail and if it exists in the database
                if (student.Username.Contains("@edu.eal.dk") && _db.Brugers.FirstOrDefault(b => b.Brugernavn.Equals(student.Username)) == null)
                {
                    classId = cls._id;
                    student.Username.Replace("@edu.eal.dk", ""); // remove the ending

                    submitChanges(_db);
                    Debug.WriteLine("Homeroom name: " + student.HomeRoomClass.Name);
                    Debug.WriteLine("Finished searching through holds, number of errors: " + numberOfErrors);

                    Navn name = new Navn
                    {
                        Fornavn = student.Name.FirstName,
                        Efternavn = student.Name.LastName
                    };

                    _db.Navns.InsertOnSubmit(name);

                    submitChanges(_db);
                    Debug.WriteLine("Finished adding a name, number of errors: " + numberOfErrors);

                    Bruger user = new Bruger
                    {
                        Navn_id = name._id,
                        Brugernavn = student.Username,
                        Password = student.Password
                    };

                    _db.Brugers.InsertOnSubmit(user);

                    submitChanges(_db);
                    Debug.WriteLine("Finished adding a user, number of errors: " + numberOfErrors);

                Studerende stud = new Studerende
                {
                    Godkendt = 0,
                    Bruger_id = user._id,
                    Hold_id = classId
                };

                _db.Studerendes.InsertOnSubmit(stud);

                submitChanges(_db);
                Debug.WriteLine("Finished adding a student, number of errors: " + numberOfErrors);
                }
                else numberOfErrors++;
            }
            else
            {
                numberOfErrors++;
                Debug.WriteLine("DEBUG: Couldn't find the specified homeroom");
            }
            var message = new HttpResponseMessage();

            if (numberOfErrors != 0)
                message.Forbidden("Errors: " + numberOfErrors);
            else message.OK("{\"Response\":\"OK\"}");

            return message;
        }
        public HttpResponseMessage Post([FromBody]Models.PossibleBooking possibleBooking)
        {
            var message = new HttpResponseMessage();
            // gets the subject from the database
            var subject = _db.GetTable<Models.Subject>().SingleOrDefault(s => s.Name == possibleBooking.Subject.Name);

            if (subject != null)
            {
                // write it to the database
                _db.GetTable<Models.PossibleBooking>().InsertOnSubmit(possibleBooking);

                // commit changes to the database
                _db.SafeSubmitChanges();
                message.OK("{\"Response\":\"OK\"}");
            }
            else message.Forbidden("Faget blev ikke fundet");

            return message;
        }