Exemple #1
0
        public UserModelRO CreateUser(UserModel user)
        {
            // check for existing user
            var existingUser = _ctx.Users.Find(user.username);

            if (existingUser != null)
            {
                ThrowWithCode(AuthException.EXISTING_USER);
            }

            // check for a valid username
            try { ValidateUsername(user.username); } catch (AuthenticationException ex) { throw ex; }
            // check for a valid password
            try { ValidatePassword(user.password); } catch (AuthenticationException ex) { throw ex; }
            user.password = HashPassword(user.password);

            // save user
            var createdUser = _ctx.Add(user);
            var saved       = _ctx.SaveChanges();

            // saved should contain the number of rows affected
            if (saved < 1)
            {
                ThrowWithCode(AuthException.USER_COULD_NOT_BE_CREATED);
            }
            // return with token
            return(new UserModelRO {
                username = (string)createdUser.Property("username").CurrentValue,
                token = AssignTokenToUser(createdUser.Entity),
                error = AuthException.OK
            });
        }
Exemple #2
0
        public ShiftModel CreateShift(ShiftModel shift)
        {
            var createdShift = ctx.Add(shift);
            var saved        = ctx.SaveChanges();

            if (saved < 1)
            {
                throw new Exception("could not be created");
            }
            return(createdShift.Entity);
        }