Пример #1
0
 public void EnsureOwnership(MtgIdentity identity)
 {
     if (!UserId.Equals(identity.Id))
     {
         throw new SecurityException($"User {identity.Id} does not have permissions to modify deck {Id}.");
     }
 }
Пример #2
0
        public async Task RemoveCardFromDeck(MtgIdentity identity, Card card, Guid deckId)
        {
            var deck = await GetDeckById(deckId);

            deck.EnsureOwnership(identity);
            HandleDeckCardStoreModification(deck, card, DeckCardStoreAction.RemoveCard);
            _context.SaveChanges();
        }
Пример #3
0
        public async Task <IEnumerable <Deck> > GetDecks(MtgIdentity identity)
        {
            var filters = new []
            {
                DeckFilters.FilterByUser(identity)
            };

            return(await _context.Decks.Filter(filters).ToListAsync());
        }
Пример #4
0
        public async Task <Deck> CreateNewDeck(MtgIdentity identity, NewDeckData newDeckData)
        {
            EnsureDeckDoesNotExist(identity, newDeckData.Title);
            var newDeck = Deck.New(identity, newDeckData);
            await _context.Decks.AddAsync(newDeck);

            _context.SaveChanges();
            return(newDeck);
        }
Пример #5
0
        public async Task <IEnumerable <DeckOverview> > GetAllDecks(MtgIdentity identity)
        {
            try
            {
                _logger.LogInformation("Getting all decks for user {userId}", identity.Id);
                var decks = await _deckRepository.GetDecks(identity);

                return(decks.Select(m => new DeckOverview(m.Id, m.Title)));
            }
            catch (Exception ex)
            {
                throw new DeckServiceException($"Something went wrong while retrieving decks for user {identity.Id}", ex);
            }
        }
Пример #6
0
        public async Task RemoveCardFromDeck(MtgIdentity identity, Guid cardId, Guid deckId)
        {
            try
            {
                // TODO(CD): Same as above
                var card = await TryCreateAndGetCard(cardId);

                await _deckRepository.RemoveCardFromDeck(identity, card, deckId);
            }
            catch (Exception ex)
            {
                throw new DeckServiceException($"Something went wrong while adding card {cardId} to deck {deckId} for user {identity.Id}", ex);
            }
        }
Пример #7
0
        public async Task AddCardToDeck(MtgIdentity identity, Guid cardId, Guid deckId)
        {
            try
            {
                // TODO(CD): Add a deck validation service to ensure rules are met for the relevant format
                // TODO(CD): Replace this call with a card service
                var card = await TryCreateAndGetCard(cardId);

                await _deckRepository.AddCardToDeck(identity, card, deckId);
            }
            catch (Exception ex)
            {
                throw new DeckServiceException($"Something went wrong while adding card {cardId} to deck {deckId} for user {identity.Id}", ex);
            }
        }
Пример #8
0
        public async Task <DeckWithCardOverview> AddNewDeck(MtgIdentity identity, string title, string description)
        {
            try
            {
                var data = new NewDeckData(title, description);
                _logger.LogInformation("Adding a new deck for {userId} with data {data}", identity.Id, data);
                var deck = await _deckRepository.CreateNewDeck(identity, data);

                return(await GetDeck(deck.Id));
            }
            catch (Exception ex)
            {
                throw new DeckServiceException($"Something went wrong while adding a new deck for user {identity.Id}", ex);
            }
        }
Пример #9
0
        private void EnsureDeckDoesNotExist(MtgIdentity identity, string title)
        {
            var filters = new []
            {
                DeckFilters.FilterByUser(identity),
                DeckFilters.FilterByTitle(title)
            };

            var matchingDecks = _context.Decks.Filter(filters: filters);

            if (Enumerable.Any(matchingDecks))
            {
                _logger.LogError("Adding a new deck for user {userId} failed as one already existed with the same title.", identity.Id);
                throw new DuplicateResourceException("A deck with this title already exists.");
            }
        }
Пример #10
0
 public static Expression <Func <Deck, bool> > FilterByUser(MtgIdentity identity)
 {
     return(m => m.UserId == identity.Id);
 }
Пример #11
0
 public static Deck New(MtgIdentity identity, NewDeckData data)
 {
     return(new (data.Title, data.Description, identity.Id));
 }