Esempio n. 1
0
        public async Task <int> Handle(CreateEpisodeCommand request, CancellationToken cancellationToken)
        {
            var entity = new Episode {
                Name = request.Name
            };

            _context.Episodes.Add(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return(entity.Id);
        }
Esempio n. 2
0
        public async Task <Unit> Handle(DeleteCharacterCommand request, CancellationToken cancellationToken)
        {
            var entity = _context.Characters.Find(request.Id)
                         ?? throw new NotFoundException(typeof(Character), request.Id);

            entity.IsActive = false;

            _context.Characters.Update(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Esempio n. 3
0
        public async Task <int> Handle(EditEpisodeCommand request, CancellationToken cancellationToken)
        {
            var entity = _context.Episodes.Find(request.Id)
                         ?? throw new NotFoundException(typeof(Episode), request.Id);

            entity = _mapper.Map(request, entity);

            _context.Episodes.Update(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return(entity.Id);
        }
Esempio n. 4
0
        //TODO Refactor handler
        public async Task <int> Handle(CreateCharacterCommand request, CancellationToken cancellationToken)
        {
            var entity   = _mapper.Map <CreateCharacterCommand, Character>(request);
            var episodes = GetCharacterEpisodes(request.EpisodesIds);
            var friends  = GetCharacterFriends(request.FriendsIds);

            entity = AttachEpisodesAndFriends(episodes, friends, entity);

            _context.Characters.Add(entity);

            await _context.SaveChangesAsync(cancellationToken);

            UpdateFriends(entity);

            return(entity.Id);
        }
        //TODO Refactor handler
        public async Task <int> Handle(EditCharacterCommand request, CancellationToken cancellationToken)
        {
            var entity = _context.Characters
                         .Include(x => x.Episodes)
                         .Include(x => x.Friends)
                         .SingleOrDefault(x => x.Id == request.Id);

            if (entity == null)
            {
                throw new NotFoundException(typeof(Character), request.Id);
            }

            entity = UpdateCharacter(entity, request);

            _context.Characters.Update(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return(entity.Id);
        }