public int CreateUser(UserModel um)
        {
            int result = DaoUtilities.NO_CHANGES;

            USER1 u = db.USER1.Create();

            u.USER_ID = um.UserId;
            u.USER_PASSWORD = hashSHA512(um.Password);
            u.USER_FIRSTNAME = um.FirstName;
            u.USER_LASTNAME = um.LastName;
            u.USER_ADR1 = um.Adress1;
            u.USER_ADR2 = um.Adress2;
            u.USER_PC = um.PC;
            u.USER_TOWN = um.Town;
            u.USER_PHONE = um.Phone;
            u.USER_MAIL = um.Mail;

            db.USER1.Add(u);
            try {
                int saveResult = db.SaveChanges();

                if (saveResult == 1)
                    result = DaoUtilities.SAVE_SUCCESSFUL;
            }
            catch (DbUpdateConcurrencyException e)
            {
                Console.WriteLine(e.GetBaseException().ToString());
                result = DaoUtilities.UPDATE_CONCURRENCY_EXCEPTION;
            }
            catch (DbUpdateException e)
            {
                Console.WriteLine(e.GetBaseException().ToString());
                result = DaoUtilities.UPDATE_EXCEPTION;
            }
            catch (DbEntityValidationException e)
            {
                Console.WriteLine(e.GetBaseException().ToString());
                result = DaoUtilities.ENTITY_VALIDATION_EXCEPTION;
            }
            catch (NotSupportedException e)
            {
                Console.WriteLine(e.GetBaseException().ToString());
                result = DaoUtilities.UNSUPPORTED_EXCEPTION;
            }
            catch (ObjectDisposedException e)
            {
                Console.WriteLine(e.GetBaseException().ToString());
                result = DaoUtilities.DISPOSED_EXCEPTION;
            }
            catch (InvalidOperationException e)
            {
                Console.WriteLine(e.GetBaseException().ToString());
                result = DaoUtilities.INVALID_OPERATION_EXCEPTION;
            }
            return result;
        }
        public int RemoveUserOfEvent(string EventId, UserModel um)
        {
            int result = DaoUtilities.NO_CHANGES;

            EVENT ev = db.EVENT.Find(EventId);

            if (ev != null)
            {
                USER1 u = db.USER1.Find(um.UserId);
                if (u != null)
                {
                    u.EVENT.Remove(ev);
                    ev.USER1.Remove(u);
                    ev.EVENT_MAXNBR = ev.EVENT_MAXNBR + 1;
                    try
                    {
                        int saveResult = db.SaveChanges();

                        if (saveResult >= 1)
                            result = DaoUtilities.SAVE_SUCCESSFUL;
                    }
                    catch (DbUpdateConcurrencyException e)
                    {
                        Console.WriteLine(e.GetBaseException().ToString());
                        result = DaoUtilities.UPDATE_CONCURRENCY_EXCEPTION;
                    }
                    catch (DbUpdateException e)
                    {
                        Console.WriteLine(e.GetBaseException().ToString());
                        result = DaoUtilities.UPDATE_EXCEPTION;
                    }
                    catch (DbEntityValidationException e)
                    {
                        Console.WriteLine(e.GetBaseException().ToString());
                        result = DaoUtilities.ENTITY_VALIDATION_EXCEPTION;
                    }
                    catch (NotSupportedException e)
                    {
                        Console.WriteLine(e.GetBaseException().ToString());
                        result = DaoUtilities.UNSUPPORTED_EXCEPTION;
                    }
                    catch (ObjectDisposedException e)
                    {
                        Console.WriteLine(e.GetBaseException().ToString());
                        result = DaoUtilities.DISPOSED_EXCEPTION;
                    }
                    catch (InvalidOperationException e)
                    {
                        Console.WriteLine(e.GetBaseException().ToString());
                        result = DaoUtilities.INVALID_OPERATION_EXCEPTION;
                    }
                }
            }
            return result;
        }
        public bool IsMember(UserModel um)
        {
            if (db.MEMBER.Find(um.UserId) != null)
                return true;

            return false;
        }
        public bool IsManager(UserModel um)
        {
            if (db.MANAGER.Find(um.UserId) != null)
                return true;

            return false;
        }
        public bool IsContributor(UserModel um)
        {
            if (db.CONTRIBUTOR.Find(um.UserId) != null)
                return true;

            return false;
        }
        public bool IsAdmin(UserModel um)
        {
            if (db.ADMIN.Find(um.UserId) != null)
                return true;

            return false;
        }
        /// <summary>
        /// Convert a user from the database to a UserModel
        /// </summary>
        /// <param name="u"></param>
        /// <returns></returns>
        public UserModel ConvertUserToUserModel(USER1 u)
        {
            UserModel um = new UserModel();

            if (u != null)
            {
                um.UserId = u.USER_ID;
                um.Password = u.USER_PASSWORD;
                um.FirstName = u.USER_FIRSTNAME;
                um.LastName = u.USER_LASTNAME;
                um.Adress1 = u.USER_ADR1;
                um.Adress2 = u.USER_ADR2;
                um.PC = u.USER_PC;
                um.Town = u.USER_TOWN;
                um.Phone = u.USER_PHONE;
                um.Mail = u.USER_MAIL;

                var userServices = new UserServices();
                um.UserRoles = new RolesModel();
                um.UserRoles.IsAdmin = userServices.IsAdmin(um);
                um.UserRoles.IsContributor = userServices.IsContributor(um);
                um.UserRoles.IsManager = userServices.IsManager(um);
                um.UserRoles.IsMember = userServices.IsMember(um);
            }
            else
                um = null;

            return um;
        }
        public IHttpActionResult Register(UserModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            var result = userServices.CreateUser(model);

               if (result == DaoUtilities.SAVE_SUCCESSFUL)
            {
                return Ok();
            }
            if(result == DaoUtilities.UPDATE_EXCEPTION)
            {
                var ex = new Exception("User already exists");
                return InternalServerError(ex);
            }
            return InternalServerError();
        }