コード例 #1
0
        public async Task<IHttpActionResult> Create(Comment model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            var user = db.Users.Find(User.Identity.GetUserId());

            model.AuthorName = user.UserName;
            model.Created = DateTimeOffset.UtcNow;

            db.Comments.Add(model);
            await db.SaveChangesAsync();

            return Ok();
        }
コード例 #2
0
        public async Task<IHttpActionResult> Edit (Comment model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var oldComment = db.Comments.AsNoTracking().FirstOrDefault(c => c.Id == model.Id);

            if (oldComment.Body != model.Body)
            {
                oldComment.Body = model.Body;
                oldComment.Updated = DateTimeOffset.UtcNow;
                await db.SaveChangesAsync();
            }

            return Ok();
        }