public async Task <IActionResult> Post([FromBody] TodoItem item)
        {
            if (item is null)
            {
                return(BadRequest());
            }

            await _context.AddAsync(item);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (Exception)
            {
                // TODO: Insert logging here

                // The new entity could not be committed to the database.
                // RFC 2616 Section 10.5 recommends that an explanation to be provided
                // for 500 responses.
                return(StatusCode(StatusCodes.Status500InternalServerError,
                                  "Could not commit new entity to the database"));
            }

            return(CreatedAtRoute("GetTodo", new { id = item.Id }, item));
        }
Пример #2
0
        public async Task <IActionResult> Add()
        {
            NewTodo.CreatedOn = DateTime.Now;
            NewTodo.User      = await _context.Users.FirstOrDefaultAsync();

            await _context.AddAsync(NewTodo);

            await _context.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }
        public async Task <Todo> AddTodoAsync(string text, bool isComplete)
        {
            var todo = new Todo
            {
                Text       = text,
                IsComplete = isComplete
            };
            await _context.AddAsync(todo);

            await _context.SaveChangesAsync();

            return(todo);
        }
Пример #4
0
        public async Task <bool> Handle(AddCommand request, CancellationToken cancellationToken)
        {
            var strategy = _dbContext.Database.CreateExecutionStrategy();

            try
            {
                await strategy.ExecuteAsync(async() =>
                {
                    using (var transaction = _dbContext.Database.BeginTransaction())
                    {
                        await _dbContext.AddAsync(request.Model);
                        _dbContext.SaveChanges();
                        transaction.Commit();
                    }
                });
            }
            catch (Exception e)
            {
                _logger.LogError(" OrderActionLogCommandHandler 操作异常" + e.Message);
                return(false);
            }
            return(true);
        }