示例#1
0
        public void PutBoardUpdatesBoard()
        {
            var controller  = new BoardController();
            var board = new Board()
                {
                    Questions = new List<Question>
                        {
                            new Question()
                                {
                                    Answers = new List<Answer>
                                        {
                                            new Answer
                                                {
                                                    Text = "A1"
                                                },
                                            new Answer
                                                {
                                                    Text = "A2"
                                                }
                                        },
                                    Text = "Question 1",
                                    Order = 1
                                }
                        },
                    Name = "Board Name"
                };

            controller.PostBoard(board);
        }
示例#2
0
        public IHttpActionResult PostBoard(Board board)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Boards.Add(board);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = board.Id }, board);
        }
示例#3
0
        public void Map(Board source)
        {
            var existingBoard = db.Boards.Where(x => x.Id == source.Id)
                                  .Include(x => x.Questions.Select(q => q.Answers))
                                  .SingleOrDefault();

            if (existingBoard == null)
                throw new InvalidOperationException(string.Format("Unable to find board with id {0} in database",source.Id));

            var questionsToBeDeleted = new List<Question>();
            var answersToBeDeleted = new List<Answer>();

            foreach (var question in existingBoard.Questions)
            {
                var sourceQuestion = source.Questions.SingleOrDefault(q => q.Id == question.Id);

                if (sourceQuestion == null)
                {
                    questionsToBeDeleted.Add(question);
                }
                else if (question.Equals(sourceQuestion) == false)
                {
                    //Question exists and has changed
                    db.Entry(question).CurrentValues.SetValues(sourceQuestion);
                }

                foreach (var answer in question.Answers)
                {
                    var sourceAnswer = sourceQuestion.Answers.SingleOrDefault(a => a.Id == answer.Id);

                    if(sourceAnswer == null)
                    {
                        answersToBeDeleted.Add(answer);
                    }
                    else if (sourceAnswer != null && answer.Equals(sourceAnswer) == false)
                    {
                        //Answer exists and has changed
                        db.Entry(answer).CurrentValues.SetValues(sourceAnswer);
                    }
                }
            }

            db.Entry(existingBoard).CurrentValues.SetValues(source);

            var newQuestions = source.Questions.Where(q => q.Id == 0);
            var newAnswers = source.Questions.SelectMany(q => q.Answers).Where(a => a.Id == 0);

            db.Questions.AddRange(newQuestions);
            db.Answers.AddRange(newAnswers);
            db.Questions.RemoveRange(questionsToBeDeleted);
            db.Answers.RemoveRange(answersToBeDeleted);
        }
示例#4
0
        public IHttpActionResult PutBoard(int id, Board board)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

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

            var mapper = new BoardMapper(db);
            mapper.Map(board);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BoardExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }
            catch(Exception e)
            {
                return InternalServerError(e);
            }

            return StatusCode(HttpStatusCode.NoContent);
        }