コード例 #1
0
        public IHttpActionResult Post(HomeworkRequestModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var homework = new Homework
            {
                Content = model.Content,
                Deadline = model.Deadline
            };

            this.data.Homeworks.Add(homework);
            this.data.Homeworks.SaveChanges();

            return this.Ok(homework);
        }
コード例 #2
0
        public IHttpActionResult Put(int id, HomeworkRequestModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var homework = this.data.Homeworks
                .All()
                .FirstOrDefault(h => h.Id == id);

            if (homework == null)
            {
                return this.BadRequest("Homework with id " + id + " does not exist!");
            }

            homework.Content = model.Content;
            homework.Deadline = model.Deadline;

            this.data.Homeworks.Update(homework);
            this.data.Homeworks.SaveChanges();

            return this.Ok(model);
        }