// GET: api/FromName public IHttpActionResult Get() { string returnValue = string.Empty; ReturnStatus returnStatus = new ReturnStatus(); FromUserRepository fromUserData = new FromUserRepository(); returnStatus = fromUserData.GetCurrentUserName(); if (returnStatus.StatusText == "OK") { return Ok(returnStatus); } else { return BadRequest(returnStatus.ErrorMessage); } }
// POST: api/FromName public IHttpActionResult Post([FromBody]From fromName) { ReturnStatus returnStatus = new ReturnStatus(); FromUserRepository fromUserData = new FromUserRepository(); returnStatus = fromUserData.UpdateFromName(fromName.FromName); if (returnStatus.Status) { return Ok(); } else { return BadRequest(returnStatus.ErrorMessage); } }
public ReturnStatus GetCurrentUserName() { string fromName = string.Empty; ReturnStatus returnStatus = new ReturnStatus(); try { using (IDbConnection db = new SqlConnection(ConfigurationValues.PostOfficeDatabaseConnection)) { //const string query = "SELECT [ID],[UserName],[FromName]" const string query = "SELECT [FromName]" + "FROM [FromName]" + " where [UserName] = @UserName"; fromName = db.Query<string>(query, new { @UserName = Utility.GetUserName() }).SingleOrDefault(); returnStatus.AdditionalInformation = fromName; returnStatus.Status = true; returnStatus.ErrorMessage = "None"; returnStatus.StatusText = "OK"; return returnStatus; } } catch (Exception er) { returnStatus.AdditionalInformation = "None"; returnStatus.Status = false; returnStatus.ErrorMessage = er.ToString(); returnStatus.StatusText = "None"; return returnStatus; } }
public ReturnStatus UpdateFromName(string newFromName) { int count = 0; ReturnStatus returnStatus = new ReturnStatus(); try { using (IDbConnection db = new SqlConnection(ConfigurationValues.PostOfficeDatabaseConnection)) { const string query = "SELECT count(ID)" + "FROM [FromName]" + " where [UserName] = @UserName"; count = db.Query<int>(query, new { @UserName = Utility.GetUserName() }).SingleOrDefault(); } if (count > 0) { using (IDbConnection db = new SqlConnection(ConfigurationValues.PostOfficeDatabaseConnection)) { const string query = "update FromName" + " set FromName = @FromName" + " where username = @username"; int rowsAffectd = db.Execute(query, new { @FromName = newFromName, @username = Utility.GetUserName() }); returnStatus.Status = true; returnStatus.ErrorMessage = "None"; returnStatus.StatusText = "OK"; return returnStatus; } } else { using (IDbConnection db = new SqlConnection(ConfigurationValues.PostOfficeDatabaseConnection)) { const string query = "insert into FromName" + "(UserName,FromName)" + "values(@UserName,@FromName)"; int rowsAffectd = db.Execute(query, new { @FromName = newFromName, @username = Utility.GetUserName() }); returnStatus.Status = true; returnStatus.ErrorMessage = "None"; returnStatus.StatusText = "OK"; return returnStatus; } } } catch (Exception er) { returnStatus.Status = false; returnStatus.ErrorMessage = er.ToString(); returnStatus.StatusText = "Error"; return returnStatus; } }