public HttpResponseMessage PostCreateUser(UserModel model) { try { var dbContext = new LibrarySystemContext(); using (dbContext) { ValidateName(model.Name); var user = new User() { Name = model.Name, AuthCode = model.AuthCode }; dbContext.Users.Add(user); dbContext.SaveChanges(); user.SessionKey = this.GenerateSessionKey(user.Id); user.UniqueNumber = user.Id + 1000; dbContext.SaveChanges(); var loggedModel = new LoggedUserModel() { UniqueNumber = user.UniqueNumber, SessionKey = user.SessionKey }; var response = this.Request.CreateResponse(HttpStatusCode.Created, loggedModel); return response; } } catch(Exception ex) { var response = this.Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message); return response; } }
public HttpResponseMessage PostLoginUser(UserModel model) { try { ValidateAuthCode(model.AuthCode); var context = new LibrarySystemContext(); using (context) { var user = context.Users.FirstOrDefault(u => u.UniqueNumber == model.UniqueNumber && u.AuthCode == model.AuthCode); if (user == null) { throw new InvalidOperationException("Грешна парола или потребителски номер"); } if (user.SessionKey == null) { user.SessionKey = this.GenerateSessionKey(user.Id); context.SaveChanges(); } var loggedModel = new LoggedUserModel() { UniqueNumber = user.UniqueNumber, SessionKey = user.SessionKey, Name = user.Name }; var response = this.Request.CreateResponse(HttpStatusCode.Created, loggedModel); return response; } } catch (Exception ex) { var response = this.Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message); return response; } }