private bool ValidateCreationResponse(UserCreationRequest req) { if (req.Email == null || req.Email.Equals("")) { return(false); } if (req.Password == null || req.Password.Equals("")) { return(false); } if (req.SecurityAnswer == null || req.SecurityAnswer.Equals("")) { return(false); } return(!(req.SecurityQuestion == null || req.SecurityQuestion.Equals(""))); }
/// <summary> /// Request for creating a base mechanic user account. Documention is found in the Web API Enumeration file /// in the /RepairJob/Requirements tab, starting at row 1 /// </summary> /// <param name="ctx">The HttpListenerContext to respond to</param> public void HandlePostRequest(HttpListenerContext ctx) { try { #region Input Validation if (!ctx.Request.HasEntityBody) { WriteBodyResponse(ctx, 400, "No Body", "Request lacked a body"); return; } UserCreationRequest req = JsonDataObjectUtil <UserCreationRequest> .ParseObject(ctx); if (req == null) { WriteBodyResponse(ctx, 400, "Incorrect Format", "Request was in the wrong format"); return; } if (!ValidateCreationResponse(req)) { WriteBodyResponse(ctx, 400, "Incorrect Format", "Not all fields of the request were filled"); return; } #endregion MySqlDataManipulator connection = new MySqlDataManipulator(); using (connection) { bool res = connection.Connect(MySqlDataManipulator.GlobalConfiguration.GetConnectionString()); if (!res) { WriteBodyResponse(ctx, 500, "Unexpected ServerError", "Connection to database failed"); return; } #region Action Handling var users = connection.GetUsersWhere(" Email = \"" + req.Email + "\""); if (users == null) { WriteBodyResponse(ctx, 500, "Unexpected Server Error", connection.LastException.Message); return; } if (users.Count > 0) { WriteBodyResponse(ctx, 409, "User Conflict", "User with email already exists"); return; } res = connection.AddUser(req.Email, req.Password, req.SecurityQuestion, req.SecurityAnswer); if (!res) { WriteBodyResponse(ctx, 500, "Unexpected ServerError", connection.LastException.Message); return; } WriteBodylessResponse(ctx, 200, "OK"); #endregion } } catch (HttpListenerException) { //HttpListeners dispose themselves when an exception occurs, so we can do no more. } catch (Exception e) { WriteBodyResponse(ctx, 500, "Internal Server Error", e.Message); } }