public async Task <IActionResult> AddToSubscriptions([FromRoute] Guid id)
        {
            var currentUser            = ControllerContext.CurrentUser();
            var discountUserPromocodes = await _discounts.AddToSubscriptionsAsync(id, currentUser.Id);

            _logger.LogInformation("Add To Subscriptions. Guid: {@id}. Result: {@userPromocodes}. User: {@currentUser}.", id, discountUserPromocodes, currentUser);

            if (discountUserPromocodes != null && !discountUserPromocodes.CurrentPromocode.IsEmpty())
            {
                var employee = (await _users.GetUserByUidAsync(currentUser.Id)).ToEmployee();

                var userEvent = new PromocodeAddedIntegrationEvent <UserMailContent>
                                    (new UserMailContent(employee, id, discountUserPromocodes.DiscountName,
                                                         discountUserPromocodes.CurrentPromocode.PromocodeValue),
                                    "WebApi", new BusParams("crazyprice.direct", "mail.user"));

                await PublishThroughEventBusAsync(userEvent);

                var companyEvent = new PromocodeAddedIntegrationEvent <CompanyMailContent>
                                       (new CompanyMailContent(discountUserPromocodes.Company, id, discountUserPromocodes.DiscountName,
                                                               discountUserPromocodes.CurrentPromocode.PromocodeValue),
                                       "WebApi", new BusParams("crazyprice.direct", "mail.company"));

                await PublishThroughEventBusAsync(companyEvent);
            }

            return(Ok(discountUserPromocodes?.UserPromocodes));
        }
        public async Task <IActionResult> GetDiscounts([FromBody, CustomizeValidator(RuleSet = "SearchCriteria")] SearchCriteria searchCriteria)
        {
            var currentUser = ControllerContext.CurrentUser();

            searchCriteria.CurrentUser = currentUser;

            if (searchCriteria.IsNotAdministratorSortByDateCreate(currentUser.Role))
            {
                _logger.LogWarning("Get Discounts. SearchCriteria: {@searchCriteria}. Result is Empty, user does not have permission. User: {@currentUser}.", searchCriteria, currentUser);
                return(Unauthorized());
            }

            var discounts = (await _discounts.GetDiscountsAsync(searchCriteria)).TransformUsersPromocodes(currentUser);

            if (discounts == null || discounts.Count == 0)
            {
                _logger.LogWarning("Get Discounts. SearchCriteria: {@searchCriteria}. Result is Empty. User: {@currentUser}.", searchCriteria, currentUser);
                return(NotFound("No discounts found."));
            }

            var discountsResponse = discounts.ToListDiscountResponse(searchCriteria.SearchLanguage);

            _logger.LogInformation("Get Discounts. SearchCriteria: {@searchCriteria}. Result: {@discountsResponse}. User: {@currentUser}.", searchCriteria, discountsResponse, currentUser);
            return(Ok(discountsResponse));
        }
        public async Task <IActionResult> DeleteFromSubscriptions([FromRoute] Guid discountId, [FromRoute] Guid promocodeId)
        {
            var currentUser    = ControllerContext.CurrentUser();
            var userPromocodes = await _discounts.RemoveFromSubscriptionsAsync(discountId, currentUser.Id, promocodeId);

            _logger.LogInformation("Delete From Subscriptions. Guid: {@discountId}. Promocode id: {@promocodeId}. Result: {@userPromocodes}. User: {@currentUser}.", discountId, promocodeId, userPromocodes, currentUser);

            return(Ok(userPromocodes));
        }
        public async Task <IActionResult> DeleteFromFavorites([FromRoute] Guid id)
        {
            var currentUser = ControllerContext.CurrentUser();
            await _discounts.RemoveFromFavoritesAsync(id, currentUser.Id);

            _logger.LogInformation("Delete From Favorites. Guid: {@id}. Result: deleted. User: {@currentUser}.", id, currentUser);

            return(Ok());
        }
        public async Task <IActionResult> DeleteDiscounts([FromBody] List <Guid> ids)
        {
            var currentUser = ControllerContext.CurrentUser();
            await _discounts.RemoveDiscountAsync(ids, currentUser.Id);

            _logger.LogInformation("Delete Discounts. Guid[]: {@ids}. Result: deleted. User: {@currentUser}", ids, currentUser);

            return(Ok());
        }
        public async Task <IActionResult> GetUsersStatistics()
        {
            var currentUser = ControllerContext.CurrentUser();

            var statistics = await _statistics.GetUsersStatistics();

            _logger.LogInformation("Get Users Statistics. Result: {@statistics}. User: {@currentUser}.", statistics, currentUser);
            return(Ok(statistics));
        }
Ejemplo n.º 7
0
        public async Task <IActionResult> UpdateMyUserInfo([FromBody, CustomizeValidator(RuleSet = "UpdateUser")] UpdateUserRequest request)
        {
            var currentUser = ControllerContext.CurrentUser();

            var updatedUser = (await _repository.GetUserByUidAsync(currentUser.Id)).UpdateUser(request);

            var updatedUserInfo = (await _repository.UpdateUserAsync(updatedUser)).ToUserInfo();

            _logger.LogInformation("Update UserInfo. Result: {@userInfo} User: {@currentUser}.", updatedUserInfo, currentUser);
            return(Ok(updatedUserInfo));
        }
        public async Task <IActionResult> AddVote([FromRoute] Guid id, [FromRoute, CustomizeValidator(RuleSet = "VoteValue")] int value)
        {
            var currentUser = ControllerContext.CurrentUser();
            var result      = await _discounts.VoteDiscountAsync(value, id, currentUser.Id);

            if (!result)
            {
                _logger.LogWarning("Add Vote. Guid: {@id}. Vote: {@value}. Result is Empty, user already voted. User id: {@currentUser}.", id, value, currentUser);
                return(Forbid());
            }

            _logger.LogInformation("Add Vote. Guid: {@id}. Vote: {@value}. Result: user voted. User: {@currentUser}.", id, value, currentUser);

            return(Ok());
        }
        public async Task <IActionResult> ExistsInSubscriptions([FromRoute] Guid id)
        {
            var currentUser    = ControllerContext.CurrentUser();
            var userPromocodes = await _discounts.GetSubscriptionsAsync(id, currentUser.Id);

            if (userPromocodes.IsEmpty())
            {
                _logger.LogInformation("Exists In Subscriptions. Guid: {@id}. Result: Not exists. User: {@currentUser}.", id, currentUser);
                return(NotFound("Discount not exists in Subscriptions."));
            }

            _logger.LogInformation("Exists In Subscriptions. Guid: {@id}. Result: Exists. User: {@currentUser}.", id, currentUser);

            return(Ok());
        }
        public async Task <IActionResult> GetSubscriptions([FromRoute] Guid id)
        {
            var currentUser    = ControllerContext.CurrentUser();
            var userPromocodes = await _discounts.GetSubscriptionsAsync(id, currentUser.Id);

            if (userPromocodes.IsEmpty())
            {
                _logger.LogWarning("Get Subscriptions. Guid: {@id}. Result is Empty. User: {@currentUser}.", id, currentUser);
                return(BadRequest("Not found the subscriptions of discount."));
            }

            _logger.LogInformation("Gets subscriptions. Guid: {@id}. Result: {@userPromocodes}. User: {@currentUser}.", id, userPromocodes, currentUser);

            return(Ok(userPromocodes));
        }
Ejemplo n.º 11
0
        public async Task <IActionResult> GetTags([FromRoute, CustomizeValidator(RuleSet = "SearchString")] string name)
        {
            var currentUser = ControllerContext.CurrentUser();

            var tags = await _repository.GetTagAsync(name);

            if (tags.IsNullOrEmpty())
            {
                _logger.LogWarning("Get Tags. Name: {name}. Result is Empty. User: {@currentUser}.", name, currentUser);
                return(NotFound("No tags found."));
            }

            _logger.LogInformation("Get Tags. Name: {name}. Result: {@tags}. User: {@currentUser}.", name, tags, currentUser);
            return(Ok(tags));
        }
        public async Task <IActionResult> ExistsVote([FromRoute] Guid id)
        {
            var currentUser = ControllerContext.CurrentUser();
            var exists      = await _discounts.ExistsVoteAsync(id, currentUser.Id);

            if (!exists)
            {
                _logger.LogInformation("Exists Vote. Guid: {@id}. Result: Not voted. User: {@currentUser}.", id, currentUser);
                return(NotFound("Discount not voted."));
            }

            _logger.LogInformation("Exists Vote. Guid: {@id}. Result: Voted. User: {@currentUser}.", id, currentUser);

            return(Ok());
        }
        public async Task <IActionResult> GetCountries([FromRoute, CustomizeValidator(RuleSet = "SearchString")] string searchCountry)
        {
            var currentUser = ControllerContext.CurrentUser();

            var countries = await _repository.GetCountriesAsync(searchCountry, searchCountry.GetLanguageFromFirstLetter());

            if (countries.IsNullOrEmpty())
            {
                _logger.LogWarning("Get countries. Country: {searchCountry}. Result is Empty. User: {@currentUser}.", searchCountry, currentUser);
                return(NotFound("No countries found."));
            }

            _logger.LogInformation("Get countries. Country: {searchCountry}. Result: {@countries}. User: {@currentUser}.", searchCountry, countries, currentUser);
            return(Ok(countries));
        }
        public async Task <IActionResult> GetCountriesAll([FromRoute] LanguageOption language)
        {
            var currentUser = ControllerContext.CurrentUser();

            var countries = await _repository.GetCountriesAsync(string.Empty, language);

            if (countries.IsNullOrEmpty())
            {
                _logger.LogWarning("Get all countries. Language: {@language}. Result Empty. User: {@currentUser}.", language, currentUser);
                return(NotFound("No countries found."));
            }

            _logger.LogInformation("Get all countries. Language: {@language}. Result: {@countries}. User: {@currentUser}.", language, countries, currentUser);
            return(Ok(countries));
        }
        public async Task <IActionResult> GetCompanyNames([FromRoute, CustomizeValidator(RuleSet = "SearchString")] string companyName)
        {
            var currentUser = ControllerContext.CurrentUser();

            var companies = await _repository.GetCompanyNamesAsync(companyName);

            if (companies.IsNullOrEmpty())
            {
                _logger.LogWarning("Get Company names. Company name: {companyName}. Result is Empty. User: {@currentUser}.", companyName, currentUser);
                return(NotFound("No company names found."));
            }

            _logger.LogInformation("Get Company names. Company name: {companyName}. Result: {@companies}. User: {@currentUser}.", companyName, companies, currentUser);
            return(Ok(companies));
        }
        public async Task <IActionResult> GetCitiesAll([FromRoute] LanguageOption language, [FromRoute, CustomizeValidator(RuleSet = "SearchString")] string searchCountry)
        {
            var currentUser = ControllerContext.CurrentUser();

            var cities = await _repository.GetCitiesAsync(searchCountry, string.Empty, language);

            if (cities.IsNullOrEmpty())
            {
                _logger.LogWarning("Get All Cities. Language: {@language}. Country: {searchCountry}. Result is Empty. User: {@currentUser}.", language, searchCountry, currentUser);
                return(NotFound("No cities found."));
            }

            _logger.LogInformation("Get All Cities. Language: {@language}. Country: {searchCountry}. Result: {@cities}. User: {@currentUser}.", language, searchCountry, cities, currentUser);
            return(Ok(cities));
        }
        public async Task <IActionResult> GetUpsertDiscount([FromRoute] Guid id)
        {
            var currentUser = ControllerContext.CurrentUser();
            var discount    = (await _discounts.GetDiscountByUidAsync(id));

            if (discount.IsEmpty())
            {
                _logger.LogWarning("Get Upsert Discount. Guid: {@id}. Result is Empty. User: {@currentUser}.", id, currentUser);
                return(NotFound("No discount found."));
            }

            var response = discount.ToUpsertDiscountRequest();

            _logger.LogInformation("Get Upsert Discount. Guid: {@id}. Result: {@response}. User: {@currentUser}.", id, response, currentUser);
            return(Ok(response));
        }
        public async Task <IActionResult> GetDiscount([FromRoute] Guid id, [FromRoute] LanguageOption language)
        {
            var currentUser = ControllerContext.CurrentUser();
            var discount    = (await _discounts.GetDiscountByUidAsync(id)).TransformUsersPromocodes(currentUser);

            if (discount.IsEmpty())
            {
                _logger.LogWarning("Get Discount. Guid: {@id}. Language: {@language}. Result is Empty.  User: {@currentUser}.", id, language, currentUser);
                return(NotFound("No discount found."));
            }

            var response = discount.Translate(language).ToDiscountResponse();

            _logger.LogInformation("Get Discount. Guid: {@id}. Language: {@language}. Result: {@response}.  User: {@currentUser}.", id, language, response, currentUser);
            return(Ok(response));
        }
Ejemplo n.º 19
0
        public async Task <IActionResult> GetCurrentUser()
        {
            var currentUser = ControllerContext.CurrentUser();

            var user = await _repository.GetUserByUidAsync(currentUser.Id);

            var employee = user.ToUserInfo();

            if (employee.IsEmpty())
            {
                _logger.LogWarning("Get Myself. Result is Empty. User: {@currentUser}.", currentUser);
                return(NotFound("No employee found."));
            }

            _logger.LogInformation("Get Myself. Result: {@employee}. User: {@currentUser}.", employee, currentUser);
            return(Ok(employee));
        }
        public async Task <IActionResult> UpsertDiscount([FromBody, CustomizeValidator(RuleSet = "UpsertDiscount")] UpsertDiscountRequest upsertDiscountRequest)
        {
            var currentUser = ControllerContext.CurrentUser();
            var user        = (await _users.GetUserByUidAsync(currentUser.Id)).ToUserLikeEmployee();

            var discount = upsertDiscountRequest.ToDiscount().AddChangeUserTime(user);

            var responseDiscount = await _discounts.UpsertDiscountAsync(discount);

            if (responseDiscount.IsEmpty())
            {
                _logger.LogWarning("Upsert Discount. UpsertDiscountRequest: {@upsertDiscountRequest}. Result is Empty. User: {@currentUser}.", upsertDiscountRequest, currentUser);
                return(BadRequest("No discounts were create or update."));
            }

            var response = responseDiscount.ToUpsertDiscountRequest();

            _logger.LogInformation("Upsert Discount. UpsertDiscountRequest: {@upsertDiscountRequest}. Result: {@response}. User: {@currentUser}.", upsertDiscountRequest, response, currentUser);
            return(Ok(response));
        }
        public async Task <IActionResult> GetDiscountsStatistics([FromBody] DiscountsStatisticsCriteria criteria)
        {
            var currentUser = ControllerContext.CurrentUser();

            if (criteria.CreateEndDate < criteria.CreateStartDate)
            {
                _logger.LogWarning("Get Discounts Statistics. Criteria: {@criteria}. Result is not enabled, user does not have permission. User: {@currentUser}.", criteria, currentUser);
                return(BadRequest("CreateEndDate field must be greater than CreateStartDate field."));
            }

            var statistics = await _statistics.GetDiscountsStatistics(criteria);

            if (statistics == null)
            {
                _logger.LogWarning("Get Discounts Statistics. Criteria: {@criteria}. Result is Empty. User: {@currentUser}.", criteria, currentUser);
                return(NotFound("Can't generate statistics for the specified criteria."));
            }

            _logger.LogInformation("Get Discounts Statistics. Criteria: {@criteria}. Result: {@statistics}. User: {@currentUser}.", criteria, statistics, currentUser);
            return(Ok(statistics));
        }