コード例 #1
0
        public IActionResult Patch(int key, [FromBody] Delta <User> delta)
        {
            IEnumerable <string> invalidPropertyNames = delta.GetChangedPropertyNames().Except(new[]
            {
                "Email", "Profile"
            });

            if (invalidPropertyNames.Any())
            {
                foreach (string propertyName in invalidPropertyNames)
                {
                    return(BadRequest(propertyName + " : This field is not allowed to be updated."));
                }
            }

            var entity = _userRepository.GetByKey(key);

            if (entity == null)
            {
                return(NotFound());
            }

            var map   = new UsersMapper();
            var model = map.FromEntity(entity);

            delta.Patch(model);

            var userEntity = map.ToEntity(model, entity);

            _userRepository.Update(userEntity);
            _userRepository.Save();

            return(Ok());
        }
コード例 #2
0
        public HttpResponseMessage RegisterUser([FromBody] UserRegisterModel user)
        {
            HttpResponseMessage responseMessage = this.PerformOperation(() =>
            {
                UserValidator.ValidateUsername(user.Username);
                UserValidator.ValidateDisplayName(user.DisplayName);
                UserValidator.ValidateAuthenticationCode(user.AuthCode);

                using (var context = this.ContextFactory.Create())
                {
                    User exstingUserEntity = context.Set <User>().FirstOrDefault(
                        u => u.Username == user.Username.ToLower() || u.DisplayName.ToLower() == user.DisplayName.ToLower());
                    if (exstingUserEntity != null)
                    {
                        throw new InvalidOperationException("User already exists!");
                    }

                    User newUserEntity = UsersMapper.ToEntity(user);
                    context.Set <User>().Add(newUserEntity);
                    context.SaveChanges();

                    newUserEntity.SessionKey = UserValidator.GenerateSessionKey(newUserEntity.ID);
                    context.SaveChanges();

                    UserLoggedModel loggedUser = UsersMapper.ToModel(newUserEntity);
                    return(this.Request.CreateResponse(HttpStatusCode.Created, loggedUser));
                }
            });

            return(responseMessage);
        }
コード例 #3
0
        public IActionResult Post([FromBody] User model)
        {
            var users = _userRepository.Find(x => x.Name == model.Username);

            if (users.Any())
            {
                return(BadRequest("The username supplied is already in use"));
            }

            var map = new UsersMapper();

            var entity = map.ToEntity(model);

            _userRepository.Add(entity);
            _userRepository.Save();

            return(Created(entity));
        }