public IHttpActionResult PutAnswer(int id, Answer answer) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != answer.Id) { return BadRequest(); } db.Entry(answer).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!AnswerExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }
public IHttpActionResult PostAnswer(Answer answer) { string idOfUserAnswering = (from r in db.Users where r.UserName == answer.UserId select r.Id).FirstOrDefault(); answer.CreatedDate = DateTime.Now; answer.ModifiedBy = "Foo"; answer.ModifiedDate = DateTime.Now; answer.CreatedBy = "Foo"; answer.UserId = idOfUserAnswering; Answer a= db.Answers.Add(answer); //Adding Points to User //UserPoint pointToUser = new UserPoint(); //pointToUser.CreatedBy = "Foo"; //pointToUser.ModifiedBy = "Foo"; //pointToUser.CreatedDate = DateTime.Now; //pointToUser.ModifiedDate = DateTime.Now; //var userInPointTable = (from r in db.UserPoints where r.UserId == idOfUserAnswering select r).FirstOrDefault(); //if (userInPointTable == null) //{ // pointToUser.UserId = idOfUserAnswering; // pointToUser.Points = 5; // db.UserPoints.Add(pointToUser); //} //else //{ // userInPointTable.Points = userInPointTable.Points + 5; // db.Entry(userInPointTable).State = EntityState.Modified; //} //Saving to database db.SaveChanges(); // creating answer view model from answer //AnswerViewModel ansvm = new AnswerViewModel(); //ansvm.AnswerDetailPlainText = a.AnswerDetailPlainText; //ansvm.AnswerDetailRichText = a.AnswerDetailRichText; //ansvm.CreatedDate = a.CreatedDate; //ansvm.Id = a.Id; //ansvm.QuestionId = a.QuestionId; //ansvm.Score = 10; //ansvm.UserId = a.UserId; //ansvm.UserName = a.User.FullName; //send SMS update an answer is posted string questionAskedBy = (from r in db.Questions where r.Id == answer.QuestionId select r.UserId).FirstOrDefault(); string questionTitle = (from r in db.Questions where r.Id == answer.QuestionId select r.QuestionTitle).FirstOrDefault(); string phoneNumber = (from r in db.Users where r.Id == questionAskedBy select r.PhoneNumber).FirstOrDefault(); if(phoneNumber != null) { string url = "http://qa97app.azurewebsites.net/client/#/questiondetails/" + answer.QuestionId; XmlDocument xDoc = ShortenURL(url); if (xDoc.GetElementsByTagName("status_code")[0].InnerText == "200") url = xDoc.GetElementsByTagName("url")[0].InnerText; string msg; if (answer.AnswerDetailPlainText.Length < 100) msg = "New answer on Q#" + answer.QuestionId + ". Answer : " + answer.AnswerDetailPlainText + ".For more details visit " + url; else msg = "New answer on Q#" + answer.QuestionId + ".For more details visit " + url; SMSController sms = new SMSController(); string SMSresponse = sms.SendSMS(phoneNumber, msg); } return CreatedAtRoute("DefaultApi", new { id = answer.Id }, answer); }