예제 #1
0
        public IHttpActionResult Postquestion(question question)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            var id = 0;
            try
            {
                id = db.questions.Max(p => p.quesID) + 1;
            }
            catch(Exception exp)
            {
                id = 1;
            }

            question.postedOn = DateTime.Now;
            question.quesID = id;

            db.questions.Add(question);
            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (questionExists(question.quesID))
                {
                    return Conflict();

                }
                else
                {
                    throw;
                }
            }

            return CreatedAtRoute("DefaultApi", new { id = question.quesID }, question);
        }
예제 #2
0
        public IHttpActionResult Putquestion(int id, question question)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != question.quesID)
            {
                return BadRequest();
            }

            db.Entry(question).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!questionExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }