Beispiel #1
0
        public async Task <TEntity> AddAsync(TEntity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException($"{nameof(AddAsync)} entity must not be null");
            }

            try
            {
                await DbContext.AddAsync(entity);

                await DbContext.SaveChangesAsync();

                return(entity);
            }
            catch (Exception ex)
            {
                throw new Exception($"{nameof(entity)} could not be saved {ex.Message}");
            }
        }
        public async Task <ActionResult <Todo> > PutTodo(int id, Todo todo)
        {
            if (!TodoExists(id))
            {
                return(NotFound());
            }

            var old_todo = GetTodo(id).Result.Value;

            //mapping old todo to new todo
            todo.Id                 = todo.Id == 0 ? old_todo.Id : todo.Id;
            todo.Title              = todo.Title == null ? old_todo.Title : todo.Title;
            todo.Description        = todo.Description == null ? old_todo.Description : todo.Description;
            todo.CompletePercentage = todo.CompletePercentage == 0 ? old_todo.CompletePercentage : todo.CompletePercentage;
            todo.Status             = todo.Status == null ? old_todo.Status : todo.Status.ToUpper();
            todo.ExpiredAt          = todo.ExpiredAt == DateTime.MinValue ? old_todo.ExpiredAt : todo.ExpiredAt;
            todo.CreatedAt          = old_todo.CreatedAt;
            todo.UpdatedAt          = DateTime.Now;


            // set status and complete percentage on complete todo
            if (todo.CompletePercentage == 100)
            {
                todo.Status = TodoConstants.STATUS_COMPLETE;
            }
            else if (todo.Status.ToUpper() == TodoConstants.STATUS_COMPLETE)
            {
                todo.CompletePercentage = 100;
            }


            // remove and add method to update value
            // old todo already mapped to new todo
            _context.Todo.Remove(old_todo);
            _context.Todo.Add(todo);
            await _context.SaveChangesAsync();

            return(todo);
        }
Beispiel #3
0
 public void UpdateTodo(Todo todoModels)
 {
     _context.Update(todoModels);
     _context.SaveChangesAsync();
 }