コード例 #1
0
        public HttpResponseMessage CreateComment(CommentModel model, 
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))]
            string accessToken)
        {
            return this.ExecuteOperationAndHandleExceptions(() =>
            {
                var context = new RecipeContext();
                var meUser = this.GetUserByAccessToken(accessToken, context);
                var recipe = this.GetRecipeById(model.RecepieId,context);

                this.ValidateComment(model);

                var commentEntity = new Comment
                {
                    Text = model.Text,
                    Recepy = recipe,
                    User = meUser
                };
                context.Comments.Add(commentEntity);
                context.SaveChanges();

                var responseModel = new CreatedComment()
                {
                    Id = commentEntity.CommentId,
                    OwnerId = commentEntity.UserId
                };
                var response = this.Request.CreateResponse(HttpStatusCode.Created, responseModel);
                return response;
            });
        }
コード例 #2
0
 private void ValidateComment(CommentModel model)
 {
     if (model == null || string.IsNullOrEmpty(model.Text))
     {
         throw new ArgumentNullException("Invalid comment!");
     }
 }