public async Task <CompanyDTO> UpdateCompany(CompanyDTO companyDTO, string vkToken) { var company = _mapper.Map <CompanyDTO, Company>(companyDTO); var companyFromDb = await _context.Companies.WhereIsNotDeleted(x => x.Id == companyDTO.Id).FirstOrDefaultAsync(); if (companyFromDb == null) { throw new BadRequestException(ExceptionCodes.COMMON_NOT_EXIST); } if (companyFromDb.Email != companyDTO.Email) { throw new BusinessLogicException(ExceptionCodes.READONLY_FIELD); } _context.SetValuesApp(companyFromDb, company); var vkId = await _vkService.GetCompanyIdFromVk(companyDTO.VkStr, vkToken); companyFromDb.VkId = vkId; var userFromDb = await _context.Users.WhereIsNotDeleted(x => x.CompanyId == companyFromDb.Id).FirstAsync(); if (!PasswordHelper.VerifyPassword(companyDTO.Password, userFromDb.PasswordHash)) { var newPasswordHash = PasswordHelper.GetPasswordHash(companyDTO.Password); userFromDb.PasswordHash = newPasswordHash; } await _context.SaveChangesAsync(); var updatedCompanyDTO = _mapper.Map <Company, CompanyDTO>(companyFromDb); return(updatedCompanyDTO); }
public async Task <int> GetAndSaveProductList(int companyId, string vkToken) { _vkApi.Authorize(new ApiAuthParams() { ApplicationId = ulong.Parse(_conf["VkApi:AppId"]), Settings = Settings.All, AccessToken = vkToken }); var company = await _context.Companies.Where(x => x.Id == companyId).FirstOrDefaultAsync(); if (company == null) { throw new BadRequestException(ExceptionCodes.COMMON_NOT_EXIST); } if (!company.VkId.HasValue) { throw new BusinessLogicException(ExceptionCodes.VK_ID_INVALID); } var count = 0; var offset = 0; var oldProductIdList = await _context.Products.WhereIsNotDeleted(x => x.CompanyId == companyId).Select(x => x.VkId).ToListAsync(); var products = await _vkApi.Markets.GetAsync(-1 *company.VkId.Value, null, ConstantsApp.VK_API_MARKET_GET_PRODUCT_PER_STEP, offset, true); //while (products.Count > 0 && offset< ConstantsApp.VK_API_MARKET_GET_PRODUCT_MAX_COUNT) //{ count += products.Count; foreach (var product in products) { if (product.Id.HasValue) { var productVkId = (long)product.Id; var productNew = new Product() { Name = product.Title, Description = product.Description, VkId = productVkId, Price = product.Price.Amount / 100, CompanyId = company.Id }; var alreadyExistProductNew = await _context.Products.Where(x => x.VkId == productVkId) .FirstOrDefaultAsync(); if (alreadyExistProductNew != null) { _context.SetValuesApp(alreadyExistProductNew, productNew); productNew = alreadyExistProductNew; } else { await _context.Products.AddAsync(productNew); } var oldProductIdListInd = oldProductIdList.FindIndex(x => x == productVkId); if (oldProductIdListInd >= 0) { oldProductIdList.RemoveAt(oldProductIdListInd); } foreach (var photo in product.Photos) { if (photo.Id.HasValue) { var photoVkId = (long)photo.Id; var vkImage = new VkImage() { VkId = photoVkId, Url = photo.Sizes.Where(x => x.Type == PhotoSizeType.Y) .Select(x => x.Url.AbsoluteUri).FirstOrDefault(), ProductId = productNew.Id }; var alreadyExistVkImage = await _context.VkImages.Where(x => x.VkId == photoVkId) .FirstOrDefaultAsync(); if (alreadyExistVkImage != null) { _context.SetValuesApp(alreadyExistVkImage, vkImage); } else { await _context.VkImages.AddAsync(vkImage); } } else { _logger.LogCritical(LoggerEventCodes.VK_API_MARKET_GET_PRODUCT_PHOTO_NO_VK_ID, $"Vk api get no vk id for photo, url={photo.PhotoSrc.AbsoluteUri}"); } } } else { _logger.LogCritical(LoggerEventCodes.VK_API_MARKET_GET_PRODUCT_NO_VK_ID, $"Vk api get no vk id for product, name={product.Title}"); } } offset += ConstantsApp.VK_API_MARKET_GET_PRODUCT_PER_STEP; //products = await _vkApi.Markets.GetAsync(-1 * group.Id, null, ConstantsApp.VK_API_MARKET_GET_PRODUCT_PER_STEP, offset); //} foreach (var oldProductId in oldProductIdList) { var oldProduct = await _context.Products.Where(x => x.VkId == oldProductId).Include(x => x.Images).FirstAsync(); foreach (var image in oldProduct.Images) { image.IsDeleted = true; } oldProduct.IsDeleted = true; } await _context.SaveChangesAsync(); return(count); }