public HttpResponseMessage RegisterUser([FromBody]UserRegisterModel user)
{
HttpResponseMessage responseMessage = this.PerformOperation(() =>
{
UserValidator.ValidateAuthenticationCode(user.AuthCode);
UserValidator.ValidateDisplayName(user.DisplayName);
UserValidator.ValidateUsername(user.Username);
var context = new BloggingSystemContext();
using (context)
{
User exstingUserEntity = context.Users.FirstOrDefault<User>(
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.Users.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;
}