예제 #1
0
        public IHttpActionResult Update(Poco.TodoItem item)
        {
            if (item.TodoItemId <= 0)
            {
                return(BadRequest("TodoItem ID is not valid!"));
            }

            if (String.IsNullOrWhiteSpace(item.Title))
            {
                return(BadRequest("Title is not valid!"));
            }

            try
            {
                using (var ctx = new ListyDbContext())
                {
                    TodoItem ti = ctx.TodoItems.SingleOrDefault(x => x.TodoItemId == item.TodoItemId);
                    if (item == null)
                    {
                        return(InternalServerError(new InvalidOperationException("Invalid TodoItem ID or User ID!")));
                    }

                    ti.Title       = item.Title;
                    ti.Description = item.Description;
                    ti.WillDoIn    = item.WillDoIn;
                    ti.Tags        = item.Tags;

                    ctx.SaveChanges();

                    return(Ok(Dto.Wrap(new Poco.TodoItem
                    {
                        TodoItemId = ti.TodoItemId,
                        UserId = ti.UserId,
                        Title = ti.Title,
                        Description = ti.Description,
                        CreationDate = ti.CreationDate,
                        WillDoIn = ti.WillDoIn,
                        Tags = ti.Tags,
                        IsComplete = ti.IsComplete,
                        CompletionDate = ti.CompletionDate
                    })));
                }
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
예제 #2
0
        public IHttpActionResult AddNew(Poco.TodoItem item)
        {
            if (item.UserId <= 0)
            {
                return(BadRequest("User ID is not valid!"));
            }

            if (String.IsNullOrWhiteSpace(item.Title))
            {
                return(BadRequest("Title is not valid!"));
            }

            try
            {
                using (var ctx = new ListyDbContext())
                {
                    TodoItem ti = new TodoItem
                    {
                        UserId       = item.UserId,
                        Title        = item.Title,
                        Description  = item.Description,
                        CreationDate = DateTime.Now.ToUniversalTime(),
                        WillDoIn     = item.WillDoIn,
                        Tags         = item.Tags
                    };

                    ctx.TodoItems.Add(ti);

                    ctx.SaveChanges();

                    return(Ok(Dto.Wrap(new Poco.TodoItem
                    {
                        TodoItemId = ti.TodoItemId,
                        UserId = ti.UserId,
                        Title = ti.Title,
                        Description = ti.Description,
                        CreationDate = ti.CreationDate,
                        WillDoIn = ti.WillDoIn,
                        Tags = ti.Tags
                    })));
                }
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }