Ejemplo n.º 1
0
        /// <summary>
        /// Delete a category item
        /// </summary>
        /// <param name="categoryIdentifier"></param>
        /// <returns></returns>
        public CategoryResponse DeleteCategory(string categoryIdentifier)
        {
            var result = new CategoryResponse
            {
                Meta = new Meta
                {
                    ResponseIdentifier = Guid.NewGuid(),
                    Date = DateTimeOffset.UtcNow.LocalDateTime
                }
            };

            Guid.TryParse(categoryIdentifier, out var _theId);

            var item = context.Categories
                       .SingleOrDefault(c => c.CategoryId == _theId);

            if (item != null)
            {
                context.Categories.Remove(item);
                context.SaveChanges();

                result.Data = item;
            }
            else
            {
                result.Meta.Messages = new List <string>
                {
                    "Category Identifier not found"
                };
            }

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Add a new category item
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public CategoryResponse AddCategory(Category request)
        {
            var result = new CategoryResponse
            {
                Meta = new Meta
                {
                    ResponseIdentifier = Guid.NewGuid(),
                    Date = DateTimeOffset.UtcNow.LocalDateTime
                }
            };

            try
            {
                context.Categories.Add(request);
                context.SaveChanges();
                result.Data = request;
            }
            catch (Exception e)
            {
                result.Meta.Messages = new List <string> {
                    e.Message
                };
            }



            return(result);
        }