public async Task <IEnumerable <Product> > ListAsync() { try { return(await _productRepository.ListAsync()); } catch (Exception ex) { _logService.LogException($"An error occurred when retrieving products: { ex.Message}", "Fetch_Products"); return(null); } }
public async Task <int> Save(List <ProductResource> products) { int id = 0; try { json = JsonConvert.SerializeObject(products.ToArray(), Formatting.Indented); filename = Path.Combine(_hostingEnvironment.ContentRootPath, _appSettings.Value.JsonSavePath, RandomString.GenerateRandomString(10) + ".json"); //write string to file System.IO.File.WriteAllText(filename, json); ProductPath path = new ProductPath { JsonFilePath = filename }; await _productPathRepository.AddAsync(path); await _unitOfWork.CompleteAsync(); id = path.Id; } catch (Exception ex) { _logService.LogException(ex, "Save File Path"); } return(id); }
public async Task <CreateUserResponse> CreateUserAsync(UserCredentialsResource user, params ERole[] userRoles) { try { var existingUser = await _userRepository.FindByEmailAsync(user.Email); if (existingUser != null) { return(new CreateUserResponse(false, "Email already in use.", null)); } User newUser = new User { Email = user.Email, Password = _passwordHasher.HashPassword(user.Password), }; await _userRepository.AddAsync(newUser, userRoles); await _unitOfWork.CompleteAsync(); newUser.Password = ""; return(new CreateUserResponse(true, null, newUser)); } catch (Exception ex) { _logService.LogException($"An error occurred when creating user : { ex.Message}", "Create_User"); return(new CreateUserResponse(false, "Error creating user.", null)); } }
public async Task <AccessToken> CreateAccessTokenAsync(User user) { try { var refreshToken = BuildRefreshToken(user); var accessToken = BuildAccessToken(user, refreshToken); // _refreshTokens.Add(refreshToken); UserToken userToken = new UserToken { RefreshToken = refreshToken.Token, Email = user.Email, Expiration = refreshToken.Expiration, AccessToken = "" }; await _userTokenRepository.AddAsync(userToken); await _unitOfWork.CompleteAsync(); return(accessToken); } catch (Exception ex) { _logService.LogException(ex, "CreateAccessTokenAsync"); return(null); } }
public async Task <IActionResult> UploadAsync() { ProductResponse result = new ProductResponse(); try { var file = Request.Form.Files[0]; //var folderName = Path.Combine("Resources", "Images"); //var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName); if (file.Length > 0) { var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); //var fullPath = Path.Combine(pathToSave, fileName); //var dbPath = Path.Combine(folderName, fileName); string fileExtension = Path.GetExtension(fileName); if (fileExtension != ".csv") { return(BadRequest("File type not allowed")); } var products = new List <ProductResource>(); using (var stream = file.OpenReadStream()) { try { products = stream.CsvToList <ProductResource>(); result = await _productService.ProcessFileUpload(products); } catch (Exception ex) { return(BadRequest(ex.Message)); } } if (result.Success) { return(Ok(result)); } } else { return(BadRequest()); } } catch (Exception ex) { _logService.LogException(ex, "Product Upload"); return(StatusCode(500, "Internal server error")); } return(BadRequest()); }