Beispiel #1
0
            public async Task <CommentDto> Handle(Command request, CancellationToken cancellationToken)
            {
                var activity = await _unitOfWork.ActivityRepo.FindFirstAsync(request.ActivityId, cancellationToken);

                if (activity == null)
                {
                    throw new CustomException(System.Net.HttpStatusCode.BadRequest, new { Activity = "Not Found" });
                }

                var user = await _unitOfWork.IdentityUserRepo.FindFirstAsync(request.UserName, cancellationToken);

                if (user == null)
                {
                    throw new CustomException(System.Net.HttpStatusCode.BadRequest, new { User = "******" });
                }

                DataModel.Comment comment = _mapperHelper.Map <Command, DataModel.Comment>(request);
                comment.AuthorId = user.AppUser.Id;

                _unitOfWork.CommentRepo.Add(comment);

                int insertCnt = await _unitOfWork.SaveAsync(cancellationToken);

                if (insertCnt > 0)
                {
                    comment.Activity = activity;
                    comment.Author   = user.AppUser;

                    CommentDto commentDto = _mapperHelper.Map <DataModel.Comment, CommentDto>(comment);
                    commentDto.UserImage = _photoAccessor.PreparePhotoUrl(commentDto.MainPhotoCloudFileName);
                    return(commentDto);
                }

                throw new Exception("Problem saving changes to database");
            }
        public async Task <AuthorModel> GetAllAuthorsAsync(FilterAuthorModel filterModel)
        {
            var responseModel = new AuthorModel();

            if (filterModel == null)
            {
                responseModel.Errors.Add(Constants.Errors.InvalidData);
                return(responseModel);
            }
            var repositoryModel = _mapperHelper.Map <FilterAuthorModel, DataFilter.BaseFilterModel>(filterModel);

            if (repositoryModel == null)
            {
                responseModel.Errors.Add(Constants.Errors.OccuredProcessing);
                return(responseModel);
            }
            var authors = await _authorRepository.GetAllAuthorsAsync(repositoryModel);

            foreach (var author in authors.Collection)
            {
                var itemModel = _mapperHelper.Map <AuthorDataModel, AuthorModelItem>(author);
                if (itemModel == null)
                {
                    responseModel.Errors.Add(Constants.Errors.OccuredProcessing);
                    continue;
                }
                responseModel.Items.Add(itemModel);
            }
            responseModel.ItemsCount = authors.CollectionCount;

            return(responseModel);
        }
            public async Task <UserDto> Handle(Query request, CancellationToken cancellationToken)
            {
                IdentityUser identityUser = await _unitOfWork.IdentityUserRepo.FindFirstAsync(request.UserName, cancellationToken);

                if (identityUser == null)
                {
                    throw new CustomException(HttpStatusCode.BadRequest);
                }

                //If recently expired token is matching with token in request please return recently generated token
                //This will be the case when two concurrent http request comes from same client and one request updates refresh token
                else if (request.RefreshToken == identityUser.PreviousRefreshToken && identityUser.PreviousRefreshTokenExpiry > HelperFunc.GetCurrentDateTime())
                {
                    return(_mapperHelper.Map <IdentityUser, UserDto>(identityUser));
                }

                //Check if current refresh token is matching and valid
                else if (request.RefreshToken == identityUser.RefreshToken && identityUser.RefreshTokenExpiry > HelperFunc.GetCurrentDateTime())
                {
                    //If current valid token is expired, generate new token and return it.
                    //else return existing token that is still valid.
                    if (request.RefreshToken == identityUser.RefreshToken)
                    {
                        identityUser.PreviousRefreshToken       = identityUser.RefreshToken;
                        identityUser.PreviousRefreshTokenExpiry = HelperFunc.GetCurrentDateTime().AddMinutes(_PREVIOUS_REFRESH_TOKEN_EXPIRES_IN_SEC);

                        identityUser.RefreshToken       = _jwtGenerator.CreateRefreshToken();
                        identityUser.RefreshTokenExpiry = HelperFunc.GetCurrentDateTime().AddDays(_REFRESH_TOKEN_EXPIRS_IN_DAYS);
                        _unitOfWork.IdentityUserRepo.Update(identityUser);
                        await _unitOfWork.SaveAsync(cancellationToken);
                    }
                    return(_mapperHelper.Map <IdentityUser, UserDto>(identityUser));
                }
                throw new CustomException(HttpStatusCode.BadRequest);
            }
        public async Task <BaseModel> SignUpAsync(UserRegistrationModel userRegModel)
        {
            var responseModel = new BaseModel();

            if (userRegModel == null ||
                string.IsNullOrWhiteSpace(userRegModel.FirstName) ||
                string.IsNullOrWhiteSpace(userRegModel.LastName) ||
                string.IsNullOrWhiteSpace(userRegModel.Email))
            {
                responseModel.Errors.Add(Constants.Errors.InvalidData);
                return(responseModel);
            }

            var existedUser = await _userRepository.GetUserByEmailAsync(userRegModel.Email);

            if (existedUser != null)
            {
                responseModel.Errors.Add(Constants.Errors.UserExisted);
                return(responseModel);
            }

            var user = _mapperHelper.Map <UserRegistrationModel, ApplicationUser>(userRegModel);

            user.UserName = $"{user.FirstName}{user.LastName}";
            user.Image    = userRegModel.Image;

            var result = (await _userRepository.SignUpAsync(user, userRegModel.Password)).ToList();

            if (result.Any())
            {
                responseModel.Errors = result.Select(x => x.Description).ToList();
            }

            return(responseModel);
        }
Beispiel #5
0
 public Task <Unit> Handle(Command request, CancellationToken cancellationToken)
 {
     Domain.Item model = _mapperHelper.Map <Command, Domain.Item>(request);
     _unitOfWork.ItemRepo.Save(model);
     _unitOfWork.Commit();
     return(Task.FromResult(Unit.Value));
 }
Beispiel #6
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                //Get existing activity from database
                DataModel.Activity dbActivity = await _unitOfWork.ActivityRepo.FindFirstAsync(request.ActivityId, cancellationToken);

                //Keep existing value as if user is not passing it from front end
                request.Category ??= dbActivity.Category;
                request.City ??= dbActivity.City;
                request.Date ??= dbActivity.Date;
                request.Description ??= dbActivity.Description;
                request.Title ??= dbActivity.Title;
                request.Venue ??= dbActivity.Venue;

                DataModel.Activity activity = _mapperHelper.Map <Command, DataModel.Activity>(request);

                _unitOfWork.ActivityRepo.Update(activity);

                int cnt = await _unitOfWork.SaveAsync(cancellationToken);

                if (cnt > 0)
                {
                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes to database");
            }
Beispiel #7
0
            public async Task <Guid> Handle(Command request, CancellationToken cancellationToken)
            {
                DataModel.Activity activity = _mapperHelper.Map <Command, DataModel.Activity>(request);
                //Generate new Id for new Entity
                activity.Id = Guid.NewGuid();
                _unitOfWork.ActivityRepo.Add(activity);

                UserActivity hostAttendee = new UserActivity
                {
                    Activity   = activity,
                    IsHost     = true,
                    DateJoined = HelperFunc.GetCurrentDateTime(),
                    AppUserId  = _userAccessor.GetCurrentUserId()
                };

                _unitOfWork.UserActivityRepo.Add(hostAttendee);

                int insertCnt = await _unitOfWork.SaveAsync(cancellationToken);

                if (insertCnt > 0)
                {
                    return(activity.Id);
                }

                throw new Exception("Problem saving changes to database");
            }
Beispiel #8
0
 private DataModel.Photo CreatePhotoModel(Command request, PhotoDto photoUploadResult)
 {
     DataModel.Photo photo = _mapperHelper.Map <Command, DataModel.Photo>(request);
     photo.Id            = photoUploadResult.Id;
     photo.AppUserId     = _userAccessor.GetCurrentUserId();
     photo.CloudFileName = photoUploadResult.CloudFileName;
     return(photo);
 }
Beispiel #9
0
            public async Task <ActivityDto> Handle(Query request, CancellationToken cancellationToken)
            {
                DataModel.Activity dbResult = await _unitOfWork.ActivityRepo.FindFirstAsync(request.ActivityId, cancellationToken);

                var response = _mapperHelper.Map <DataModel.Activity, ActivityDto>(dbResult);

                return(response);
            }
Beispiel #10
0
            public async Task <UserDto> Handle(Command command, CancellationToken cancellationToken)
            {
                await Validate(command);

                AppUser appUser = _mapperHelper.Map <Command, AppUser>(command);

                _unitOfWork.AppUserRepo.Add(appUser);

                int insertCnt = await _unitOfWork.SaveAsync(cancellationToken);

                if (insertCnt > 0)
                {
                    return(_mapperHelper.Map <AppUser, UserDto>(appUser));
                }

                throw new Exception("Problem saving changes to database");
            }
Beispiel #11
0
        public async Task <PrintingEditionModel> CreatePrintingEditionAsync(PrintingEditionModelItem printingEditionsModelItem)
        {
            var responseModel = ValidateData(printingEditionsModelItem);

            if (responseModel.Errors.Any())
            {
                return(responseModel);
            }

            var printingEdition = _mapperHelper.Map <PrintingEditionModelItem, PrintingEdition>(printingEditionsModelItem);

            printingEdition.Price = _currencyConverterHelper.Convert(printingEditionsModelItem.Currency, Enums.Currency.USD, printingEditionsModelItem.Price);

            printingEdition.Currency = Enums.Currency.USD;

            var authorsId = printingEditionsModelItem.Authors.Select(x => x.Id).ToArray();

            if (authorsId == null || !authorsId.Any())
            {
                responseModel.Errors.Add(Constants.Errors.InvalidData);
                return(responseModel);
            }

            var createResult = await _printingEditionRepository.CreateAsync(printingEdition);

            if (createResult.Equals(0))
            {
                responseModel.Errors.Add(Constants.Errors.FailedCreate);
                return(responseModel);
            }

            var createAuthorsInPrintingEditionResult = await _authorInPrintingEditionRepository.CreateAuthorsInPrintingEditionAsync(printingEdition.Id, authorsId);

            if (createAuthorsInPrintingEditionResult.Equals(0))
            {
                responseModel.Errors.Add(Constants.Errors.FailedCreate);
                return(responseModel);
            }

            return(responseModel);
        }
            public async Task <UserDto> Handle(Query query, CancellationToken cancellationToken)
            {
                Guid    userId   = _userAccessor.GetCurrentUserId();
                string  userName = _userAccessor.GetCurrentUserName();
                AppUser user     = await _unitOfWork.AppUserRepo.FindFirstAsync(e => e.Id == userId,
                                                                                new List <string> {
                    nameof(AppUser.Photos), nameof(AppUser.IdentityUser)
                },
                                                                                cancellationToken);

                return(_mapperHelper.Map <AppUser, UserDto>(user));
            }
Beispiel #13
0
        public async Task <UserModel> GetAllUsersAsync(FilterUserModel filterModel)
        {
            var userModel = new UserModel();

            var repositoryModel = _mapperHelper.Map <FilterUserModel, DataFilter.FilterUserModel>(filterModel);

            if (repositoryModel == null)
            {
                userModel.Errors.Add(Constants.Errors.OccuredProcessing);
                return(userModel);
            }

            var filteringUsers = await _userRepository.GetFilteredDataAsync(repositoryModel);

            if (filteringUsers == null)
            {
                userModel.Errors.Add(Constants.Errors.OccuredProcessing);
                return(userModel);
            }

            foreach (var user in filteringUsers.Collection)
            {
                var userModelItem = user.MapToModel <UserModelItem>();

                if (userModelItem == null)
                {
                    userModel.Errors.Add(Constants.Errors.OccuredProcessing);
                    continue;
                }

                userModel.Items.Add(userModelItem);
            }

            userModel.ItemsCount = filteringUsers.CollectionCount;

            return(userModel);
        }
Beispiel #14
0
        public async Task <ProfileDto> ReadProfile(Guid appUserId, CancellationToken cancellationToken)
        {
            AppUser appUser = await _unitOfWork.AppUserRepo.GetUserProfile(appUserId, cancellationToken);

            ProfileDto profileDto = _mapperHelper.Map <AppUser, ProfileDto>(appUser);

            profileDto.FollowersCount = await _unitOfWork.UserFollowerRepo.GetFollowersCountAsync(appUserId, cancellationToken);

            profileDto.FollowingCount = await _unitOfWork.UserFollowerRepo.GetFollowingCountAsync(appUserId, cancellationToken);

            profileDto.Following = await _unitOfWork.UserFollowerRepo
                                   .HasAnyAsync(f => f.Follower.Id == _userAccessor.GetCurrentUserId() &&
                                                f.UserId == appUserId, cancellationToken);

            return(profileDto);
        }
Beispiel #15
0
            public async Task <string> Handle(Command request, CancellationToken cancellationToken)
            {
                Guid    appUserId = _userAccessor.GetCurrentUserId();
                AppUser dbAppUser = await _unitOfWork.AppUserRepo.FindFirstAsync(e => e.Id == appUserId, null, cancellationToken);

                await Validate(request, dbAppUser.Id);

                AppUser appUser = _mapperHelper.Map <Command, AppUser>(request);

                appUser.Id = dbAppUser.Id;
                _unitOfWork.AppUserRepo.Update(appUser);
                int insertCnt = await _unitOfWork.SaveAsync(cancellationToken);

                if (insertCnt > 0)
                {
                    return(appUser.DisplayName);
                }

                throw new Exception("Problem saving changes to database");
            }
Beispiel #16
0
            public async Task <UserDto> Handle(Command request, CancellationToken cancellationToken)
            {
                IdentityUser identityUser = await _unitOfWork.IdentityUserRepo.FindFirstAsync(request.UserName, cancellationToken);

                if (identityUser == null)
                {
                    throw new CustomException(HttpStatusCode.Unauthorized);
                }

                if (_cryptoHelper.GenerateHash(request.Password, identityUser.Salt) == identityUser.Passoword)
                {
                    identityUser.PreviousRefreshToken       = null;
                    identityUser.PreviousRefreshTokenExpiry = null;
                    identityUser.RefreshToken       = _jwtGenerator.CreateRefreshToken();
                    identityUser.RefreshTokenExpiry = HelperFunc.GetCurrentDateTime().AddDays(30);
                    _unitOfWork.IdentityUserRepo.Update(identityUser);

                    await _unitOfWork.SaveAsync(cancellationToken);

                    return(_mapperHelper.Map <IdentityUser, UserDto>(identityUser));
                }
                throw new CustomException(HttpStatusCode.Unauthorized);
            }
Beispiel #17
0
        public async Task <OrderModel> GetOrdersAsync(FilterOrderModel filterOrder, string userId)
        {
            var responseModel = new OrderModel();

            var repositoryFilter = _mapperHelper.Map <FilterOrderModel, DataFilter.FilterOrderModel>(filterOrder);

            if (!long.TryParse(userId, out long _userId) || _userId == 0)
            {
                responseModel.Errors.Add(Constants.Errors.UserNotFound);
                return(responseModel);
            }

            var orders = await _orderRepository.GetAllOrdersAsync(repositoryFilter, _userId);

            responseModel.ItemsCount = orders.CollectionCount;

            foreach (var order in orders.Collection)
            {
                var orderModelItem = order.MapToModel();
                responseModel.Items.Add(orderModelItem);
            }

            return(responseModel);
        }
        public async Task <List <VehicleEncyclopedia> > GetWotEncyclopediaVehiclesAsync()
        {
            var tankopedia = await _webApiClient.GetResponse <Dictionary <string, WotEncyclopediaVehiclesResponse> >(
                _requestBuilder.BaseAddress,
                _requestBuilder.BuildRequestUrl(RequestType.EncyclopediaVehicles,
                                                new RequestParameter
            {
                ParameterType  = ParameterType.Fields,
                ParameterValue = "tank_id,name,tier,nation,type,description,is_premium,cost,images,next_tanks"
            }));

            var allVehicles = tankopedia.Values.ToList();

            allVehicles.AddMarkI();
            allVehicles.AddHetzerKame();

            return(_mapper.Map <List <WotEncyclopediaVehiclesResponse>, List <VehicleEncyclopedia> >(allVehicles));
        }
Beispiel #19
0
 public Task <Dto.Item> Handle(Query request, CancellationToken cancellationToken)
 {
     Domain.Item dbResponse = _unitOfWork.ItemRepo.Find(request.Name);
     return(Task.FromResult(_mapperHelper.Map <Domain.Item, Dto.Item>(dbResponse)));
 }