예제 #1
0
        public ViewTodos Handle(PostTodosCommand request)
        {
            var user = _context.Users.FirstOrDefault(u => u.Id == request.UserId);

            if (user == null)
            {
                throw new Exception("User not found.");
            }

            foreach (var todo in request.Todos)
            {
                var x = new Todo
                {
                    Detail = todo.Detail,
                    Done   = todo.Done,
                    User   = user
                };
                _context.Todos.Add(x);
                _context.SaveChanges();
                todo.Id = x.Id;
            }

            return(new ViewTodos
            {
                Todos = request.Todos,
                UserId = request.UserId
            });
        }
        public ViewTodos Handle(PutTodosCommand request)
        {
            foreach (var todo in request.Todos)
            {
                var x = _context.Todos.Include(t => t.User).FirstOrDefault(t => t.Id == todo.Id);

                if (x == null)
                {
                    continue;
                }
                if (x.User.Id != request.UserId)
                {
                    throw new Exception("User's task not found.");
                }

                x.Detail = todo.Detail;
                x.Done   = todo.Done;
            }

            _context.SaveChanges();
            return(new ViewTodos {
                Todos = request.Todos, UserId = request.UserId
            });
        }