Exemple #1
0
        protected override async Task Handle(SharePrivateList command)
        {
            var privateList = await _privateListFetchHandler.Handle(new GetPrivateListByUserId(command.UserId));

            var sharedList = CreateSharedListFromPrivate(privateList, command);

            var user = await WriteService.GetAsync <User>(command.UserId);

            var listNameSpecification = new SharedListNameSpecification(user);

            if (!listNameSpecification.SatisfiedBy(sharedList.Name))
            {
                throw new ObjectAlreadyExistsException <BookList>(new OnExceptionObjectDescriptor
                {
                    ["Name"] = command.Name
                });
            }

            await WriteService.SaveAsync(sharedList);

            var privateItems = await _privateItemsFetchHandler.Handle(new GetItemsByListId(privateList.Id));

            var sharedItems = CreateSharedItemsFromPrivateItems(privateItems, sharedList.Id);

            await WriteService.SaveBatchAsync(sharedItems);
        }
Exemple #2
0
        protected override PrivateBookListDto Convert(BookList entity, UpdatePrivateList command)
        {
            var items = Mapper.Map <IEnumerable <PrivateBookListItem>, IEnumerable <PrivateBookListItemDto> >(
                _itemsFetchHandler.Handle(new GetItemsByListId(entity.Id)).RunSync()).ToList();

            return(Mapper.Map <BookList, PrivateBookListDto>(entity,
                                                             options => options.AfterMap((wm, listDto) => listDto.Items = items)));
        }
Exemple #3
0
        protected override async Task <int> GetBookListId(AddPrivateItem command)
        {
            var list = await _listFetchHandler.Handle(new GetPrivateListByUserId(command.UserId));

            if (list == null)
            {
                throw new ObjectNotExistForException <BookList, User>(null, new OnExceptionObjectDescriptor
                {
                    ["Id"] = command.UserId.ToString()
                });
            }

            return(list.Id);
        }
Exemple #4
0
        protected override async Task Handle(AddBookToLists command)
        {
            var lists = await _listsFetchHandler.Handle(new GetListsByIds(command.ListsIds));

            var items =
                (await _itemsFetchHandler.Handle(new GetItemsByBookAndListIds(command.ListsIds, command.BookId)))
                .ToList();

            var newItems = new List <BookListItem>();

            foreach (var list in lists)
            {
                Validate(list, command.UserId, items, command.BookId);
                newItems.Add(CreateItem(list, command));
            }

            await WriteService.SaveBatchAsync(newItems);
        }
Exemple #5
0
        protected override async Task <AuthenticationDataDto> Handle(RegisterUser command)
        {
            var user = await _userFetchHandler.Handle(new GetUserByLogin(command.Email));

            if (user != null)
            {
                throw new ObjectAlreadyExistsException <User>(new OnExceptionObjectDescriptor
                {
                    ["Email"] = command.Email
                });
            }

            user = new User
            {
                Login    = command.Email,
                Password = _encryptionService.Encrypt(command.Password),
                RoleId   = (int)UserRole.User,
                Profile  = new Profile {
                    Email = command.Email
                }
            };

            await WriteService.SaveAsync(user);

            await WriteService.SaveAsync(new BookList
            {
                Name    = "Default",
                OwnerId = user.Id,
                Type    = BookListType.Private
            });

            if (_encryptionService.Encrypt(command.Password) != user.Password)
            {
                throw new WrongPasswordException();
            }

            return(_authenticationService.Authenticate(user));
        }
Exemple #6
0
        private async Task <bool> DoItemExist(int bookId, int bookListId)
        {
            var item = await _itemFetchHandler.Handle(new GetBookListItem(bookId, bookListId));

            return(item != null);
        }
Exemple #7
0
 protected override async Task <BookList> GetEntity(UpdatePrivateList command)
 {
     return(await _listFetchHandler.Handle(new GetPrivateListByUserId(command.UserId)));
 }