Beispiel #1
0
 public async Task<IHttpActionResult> PostQuestion(Question question)
 {
     if (!ModelState.IsValid)
     {
         return BadRequest(ModelState);
     }
     question.time = DateTime.UtcNow;
     question.postedBy = User.Identity.GetUserId();
     db.Questions.Add(question);
     await db.SaveChangesAsync();
     return CreatedAtRoute("DefaultApi", new { id = question.Id }, question);
 }
        public void SaveQuestionTags(string s, Question q, bool update = false)
        {
            if (update)
            {
                var adid = q.Id;
                var adtags = db.QuestionTags.Where(x => x.questionId.Equals(adid)).ToList();
                foreach (var cc in adtags)
                {
                    db.QuestionTags.Remove(cc);
                }
                db.SaveChanges();
            }
            string[] values = s.Split(',');
            Inspinia_MVC5_SeedProject.Models.Tag[] tags = new Inspinia_MVC5_SeedProject.Models.Tag[values.Length];
            QuestionTag[] qt = new  QuestionTag[values.Length];
            for (int i = 0; i < values.Length; i++)
            {
                values[i] = values[i].Trim();
                string ss = values[i];
                if (ss != "")
                {
                    var data = db.Tags.FirstOrDefault(x => x.name.Equals(ss, StringComparison.OrdinalIgnoreCase));

                    tags[i] = new Inspinia_MVC5_SeedProject.Models.Tag();
                    if (data != null)
                    {
                        tags[i].Id = data.Id;
                    }
                    else
                    {
                        tags[i].name = values[i];
                        tags[i].time = DateTime.UtcNow;
                        tags[i].createdBy = System.Web.HttpContext.Current.User.Identity.GetUserId();
                        db.Tags.Add(tags[i]);
                    }
                }
                else
                {
                    tags[i] = null;
                }
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception e)
            {
                string sb = e.ToString();
            }
            for (int i = 0; i < values.Length; i++)
            {
                if (tags[i] != null)
                {
                    qt[i] = new  QuestionTag();
                    qt[i].questionId = q.Id;
                    qt[i].tagId = tags[i].Id;
                    db.QuestionTags.Add(qt[i]);
                }
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception e)
            {
                string sb = e.ToString();
            }
        }
Beispiel #3
0
        // PUT api/Forum/5
        public async Task<IHttpActionResult> PutQuestion(int id, Question question)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

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

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

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!QuestionExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }