예제 #1
0
        public async Task <Category> AddCategoryAsync(AddCategoryInput input, [ScopedService] ApplicationDbContext _context)
        {
            Category new_category = new Category {
                Name = input.name
            };

            _context.Categories.Add(new_category);
            await _context.SaveChangesAsync();

            return(new_category);
        }
예제 #2
0
        public async Task <AddCategoryPayload> AddCategoryAsync(AddCategoryInput input, [ScopedService] AppDbContext context)
        {
            var category = new Category
            {
                Name = input.name
            };

            context.Add(category);
            await context.SaveChangesAsync();

            return(new AddCategoryPayload(category));
        }
        public async Task <AddCategoryPayload> AddCategoryAsync(AddCategoryInput input, [ScopedService] ApplicationDbContext context, CancellationToken cancellationToken)
        {
            var(name, description, parentId) = input;

            if (string.IsNullOrEmpty(name))
            {
                return(new (new UserError("The name cannot be empty.", "NAME_EMPTY")));
            }

            var attr = new Category(name, description, parentId);

            await context.Categories.AddAsync(attr, cancellationToken);

            await context.SaveChangesAsync(cancellationToken);

            return(new (attr));
        }
예제 #4
0
        public async Task <Payload <Book> > AddCategoryAsync(
            AddCategoryInput input,
            [ScopedService] BookRefDbContext context)
        {
            // Primary checks
            var book     = context.Books.Find(input.BookId);
            var category = context.Categories.Find(input.CategoryId);

            try
            {
                book.AddCategory(category, input.IsPrimary);
            }
            catch (System.Exception ex)
            {
                return(new Payload <Book>(BuildSingleError(ex)));
            }

            await context.SaveChangesAsync();

            return(new Payload <Book>(book));
        }