public async Task <bool> Update(int userId, ClientDto clientDto) { // TODO: [TESTS] (ClientService.Update) Add tests var builder = new ServiceMetricBuilder(nameof(ClientService), nameof(Update)) .WithCategory(MetricCategory.Client, MetricSubCategory.Update) .WithCustomInt1(userId) .WithCustomInt2(clientDto.ClientId); try { using (builder.WithTiming()) { ClientEntity dbEntry; using (builder.WithCustomTiming1()) { builder.IncrementQueryCount(); dbEntry = await _clientRepo.GetById(clientDto.ClientId); builder.CountResult(dbEntry); } if (dbEntry == null) { // TODO: [HANDLE] (ClientService.Update) Handle not found return(false); } if (dbEntry.UserId != userId) { // TODO: [HANDLE] (ClientService.Update) Handle wrong owner return(false); } using (builder.WithCustomTiming2()) { builder.IncrementQueryCount(); if (await _clientRepo.Update(clientDto.ToDbEntity()) <= 0) { return(false); } builder.IncrementResultsCount(); return(true); } } } catch (Exception ex) { _logger.LogUnexpectedException(ex); builder.WithException(ex); return(false); } finally { await _metrics.SubmitPointAsync(builder.Build()); } }
public async Task <List <IntListItem> > GetProjectsAsList(int userId, int productId) { // TODO: [TESTS] (ProjectService.GetProjectsAsList) Add tests var builder = new ServiceMetricBuilder(nameof(ProjectService), nameof(GetProjectsAsList)) .WithCategory(MetricCategory.Project, MetricSubCategory.GetList) .WithCustomInt1(userId) .WithCustomInt2(productId) .WithCustomInt3(0); try { using (builder.WithTiming()) { List <ProjectEntity> dbEntries; using (builder.WithCustomTiming1()) { builder.IncrementQueryCount(); dbEntries = await _projectRepo.GetAllForProduct(productId); builder.WithResultsCount(dbEntries?.Count ?? 0); } if (dbEntries == null || dbEntries.Count <= 0) { // TODO: [HANDLE] (ProjectService.GetProjectsAsList) Handle this return(new List <IntListItem>()); } if (dbEntries.First().UserId != userId) { // TODO: [HANDLE] (ProjectService.GetProjectsAsList) Handle this return(new List <IntListItem>()); } return(dbEntries .AsQueryable() .Select(project => new IntListItem { Name = project.ProjectName, Value = project.ProjectId }) .ToList()); } } catch (Exception ex) { _logger.LogUnexpectedException(ex); builder.WithException(ex); return(new List <IntListItem>()); } finally { await _metrics.SubmitPointAsync(builder.Build()); } }
public async Task <ProjectDto> GetById(int userId, int projectId) { // TODO: [TESTS] (ProjectService.GetById) Add tests var builder = new ServiceMetricBuilder(nameof(ProjectService), nameof(GetById)) .WithCategory(MetricCategory.Project, MetricSubCategory.GetById) .WithCustomInt1(userId) .WithCustomInt2(0) .WithCustomInt3(projectId); try { using (builder.WithTiming()) { ProjectEntity dbEntry; using (builder.WithCustomTiming1()) { builder.IncrementQueryCount(); dbEntry = await _projectRepo.GetById(projectId); builder.CountResult(dbEntry); } if (dbEntry == null) { // TODO: [HANDLE] (ProjectService.GetById) Handle this return(null); } builder.WithCustomInt2(dbEntry.ProductId); if (dbEntry.UserId != userId) { // TODO: [HANDLE] (ProjectService.GetById) Handle this return(null); } return(ProjectDto.FromEntity(dbEntry)); } } catch (Exception ex) { _logger.LogUnexpectedException(ex); builder.WithException(ex); return(null); } finally { await _metrics.SubmitPointAsync(builder.Build()); } }
public async Task <AuthenticationResponse> Authenticate(AuthenticationRequest request) { // TODO: [TESTS] (UserService.Authenticate) Add tests var builder = new ServiceMetricBuilder(nameof(UserService), nameof(Authenticate)) .WithCategory(MetricCategory.User, MetricSubCategory.GetSingle) .WithCustomTag1(request.Username); try { using (builder.WithTiming()) { UserEntity loggedInUser; using (builder.WithCustomTiming1()) { builder.IncrementQueryCount(); loggedInUser = await LoginUser(request.Username, request.Password); builder.CountResult(loggedInUser); } if (loggedInUser == null) { return(null); } builder.WithCustomInt1(loggedInUser.UserId); return(new AuthenticationResponse { FirstName = loggedInUser.FirstName, LastName = loggedInUser.LastName, UserId = loggedInUser.UserId, Username = loggedInUser.Username, Token = GenerateJwtToken(loggedInUser.UserId) }); } } catch (Exception ex) { _logger.LogUnexpectedException(ex); builder.WithException(ex); return(null); } finally { await _metrics.SubmitPointAsync(builder.Build()); } }
// Interface methods public async Task <UserDto> GetFromToken(string token) { // TODO: [TESTS] (UserService.GetFromToken) Add tests var builder = new ServiceMetricBuilder(nameof(UserService), nameof(GetFromToken)) .WithCategory(MetricCategory.User, MetricSubCategory.Extract); try { using (builder.WithTiming()) { // Try extract the current userId int userId; using (builder.WithCustomTiming1()) { userId = ExtractUserId(token); if (userId == 0) { return(null); } } UserEntity userEntity; using (builder.WithCustomTiming2()) { builder.IncrementQueryCount(); userEntity = await _userRepo.GetUserById(userId); builder.CountResult(userEntity); } return(userEntity == null ? null : UserDto.Projection.Compile()(userEntity)); } } catch (Exception ex) { _logger.LogUnexpectedException(ex); builder.WithException(ex); return(null); } finally { await _metrics.SubmitPointAsync(builder.Build()); } }
public async Task <bool> UpdateProduct(int userId, ProductDto productDto) { // TODO: [TESTS] (ProductService.UpdateProduct) Add tests // TODO: [VALIDATION] (ProductService.UpdateProduct) Ensure that user owns this product var builder = new ServiceMetricBuilder(nameof(ProductService), nameof(UpdateProduct)) .WithCategory(MetricCategory.Product, MetricSubCategory.Update) .WithCustomInt1(userId) .WithCustomInt2(productDto.ClientId); try { using (builder.WithTiming()) { if (productDto.UserId != userId) { // TODO: [HANDLE] (ProductService.UpdateProduct) Handle this better return(false); } using (builder.WithCustomTiming1()) { builder.IncrementQueryCount(); if (await _productRepo.Update(productDto.AsProductEntity()) <= 0) { return(false); } builder.WithResultsCount(1); return(true); } } } catch (Exception ex) { _logger.LogUnexpectedException(ex); builder.WithException(ex); return(false); } finally { await _metrics.SubmitPointAsync(builder.Build()); } }
public async Task <List <ClientDto> > GetAll(int userId) { // TODO: [TESTS] (ClientService.GetAll) Add tests var builder = new ServiceMetricBuilder(nameof(ClientService), nameof(GetAll)) .WithCategory(MetricCategory.Client, MetricSubCategory.GetAll) .WithCustomInt1(userId); try { using (builder.WithTiming()) { List <ClientEntity> dbClients; using (builder.WithCustomTiming1()) { builder.IncrementQueryCount(); dbClients = await _clientRepo.GetAll(userId); builder.WithResultsCount(dbClients?.Count ?? 0); } if (dbClients == null || dbClients.Count == 0) { return(new List <ClientDto>()); } return(dbClients .AsQueryable() .Select(ClientDto.Projection) .ToList()); } } catch (Exception ex) { _logger.LogUnexpectedException(ex); builder.WithException(ex); return(new List <ClientDto>()); } finally { await _metrics.SubmitPointAsync(builder.Build()); } }
public async Task <List <IntListItem> > GetAsListItems(int userId) { // TODO: [TESTS] (ClientService.GetAsListItems) Add tests var builder = new ServiceMetricBuilder(nameof(ClientService), nameof(GetAsListItems)) .WithCategory(MetricCategory.Client, MetricSubCategory.GetList) .WithCustomInt1(userId); try { using (builder.WithTiming()) { List <ClientEntity> clientEntries; using (builder.WithCustomTiming1()) { builder.IncrementQueryCount(); clientEntries = await _clientRepo.GetAll(userId); builder.WithResultsCount(clientEntries.Count); } return(clientEntries .AsQueryable() .Select(clientEntry => new IntListItem { Value = clientEntry.ClientId, Name = clientEntry.ClientName }) .ToList()); } } catch (Exception ex) { _logger.LogUnexpectedException(ex); builder.WithException(ex); return(new List <IntListItem>()); } finally { await _metrics.SubmitPointAsync(builder.Build()); } }
// Interface methods public async Task <RawOptions> GenerateOptions(string category, int userId) { // TODO: [TESTS] (OptionsService.GetRawOptionsForCategory) Add tests var builder = new ServiceMetricBuilder(nameof(OptionsService), nameof(GenerateOptions)) .WithCategory(MetricCategory.Option, MetricSubCategory.Generate) .WithCustomInt1(userId) .WithCustomTag1(category); var generated = new RawOptions(category); try { using (builder.WithTiming()) { List <OptionEntity> dbOptions; using (builder.WithCustomTiming1()) { builder.IncrementQueryCount(); dbOptions = await _optionRepo.GetRawOptionsForCategory(category, userId); builder.WithResultsCount(dbOptions.Count); } foreach (var dbOption in dbOptions) { generated.AddOption(dbOption); } } } catch (Exception ex) { _logger.LogUnexpectedException(ex); builder.WithException(ex); } finally { await _metrics.SubmitPointAsync(builder.Build()); } return(generated); }
public async Task <bool> AddProject(int userId, ProjectDto projectDto) { // TODO: [TESTS] (ProjectService.AddProject) Add tests // TODO: [VALIDATION] (ProjectService.AddProject) Add validation var builder = new ServiceMetricBuilder(nameof(ProjectService), nameof(AddProject)) .WithCategory(MetricCategory.Project, MetricSubCategory.Add) .WithCustomInt1(userId) .WithCustomInt2(projectDto.ProductId) .WithCustomInt3(0); try { using (builder.WithTiming()) { var projectEntity = projectDto.AsProjectEntity(); projectEntity.UserId = userId; using (builder.WithCustomTiming1()) { builder.IncrementQueryCount(); if (await _projectRepo.Add(projectEntity) <= 0) { return(false); } builder.WithResultsCount(1); return(true); } } } catch (Exception ex) { _logger.LogUnexpectedException(ex); builder.WithException(ex); return(false); } finally { await _metrics.SubmitPointAsync(builder.Build()); } }
public async Task <List <ProjectDto> > GetAllForProduct(int userId, int productId) { // TODO: [TESTS] (ProjectService.GetAllForProduct) Add tests // TODO: [VALIDATION] (ProjectService.GetAllForProduct) Ensure that the current user can see these var builder = new ServiceMetricBuilder(nameof(ProjectService), nameof(GetAllForProduct)) .WithCategory(MetricCategory.Project, MetricSubCategory.GetAll) .WithCustomInt1(userId) .WithCustomInt2(productId) .WithCustomInt3(0); try { using (builder.WithTiming()) { List <ProjectEntity> dbEntries; using (builder.WithCustomTiming1()) { builder.IncrementQueryCount(); dbEntries = await _projectRepo.GetAllForProduct(productId); builder.WithResultsCount(dbEntries.Count); } return(dbEntries .AsQueryable() .Select(ProjectDto.Projection) .ToList()); } } catch (Exception ex) { _logger.LogUnexpectedException(ex); builder.WithException(ex); return(new List <ProjectDto>()); } finally { await _metrics.SubmitPointAsync(builder.Build()); } }
public async Task <bool> AddClient(int userId, ClientDto clientDto) { // TODO: [TESTS] (ClientService.AddClient) Add tests var builder = new ServiceMetricBuilder(nameof(ClientService), nameof(AddClient)) .WithCategory(MetricCategory.Client, MetricSubCategory.Add) .WithCustomInt1(userId); try { using (builder.WithTiming()) { var clientEntity = clientDto.ToDbEntity(); clientEntity.UserId = userId; using (builder.WithCustomTiming1()) { builder.IncrementQueryCount(); if (await _clientRepo.Add(clientEntity) <= 0) { return(false); } builder.WithResultsCount(1); return(true); } } } catch (Exception ex) { _logger.LogUnexpectedException(ex); builder.WithException(ex); return(false); } finally { await _metrics.SubmitPointAsync(builder.Build()); } }
public async Task <bool> UpdateProject(int userId, ProjectDto projectDto) { // TODO: [TESTS] (ProjectService.UpdateProject) Add tests // TODO: [VALIDATE] (ProjectService.UpdateProject) Ensure that the user can edit this var builder = new ServiceMetricBuilder(nameof(ProjectService), nameof(UpdateProject)) .WithCategory(MetricCategory.Project, MetricSubCategory.Update) .WithCustomInt1(userId) .WithCustomInt2(projectDto.ProductId) .WithCustomInt3(projectDto.ProjectId); try { using (builder.WithTiming()) { ProjectEntity dbEntry; using (builder.WithCustomTiming1()) { builder.IncrementQueryCount(); dbEntry = await _projectRepo.GetById(projectDto.ProjectId); builder.CountResult(dbEntry); } if (dbEntry == null) { // TODO: [HANDLE] (ProjectService.UpdateProject) Handle this return(false); } if (dbEntry.UserId != userId) { // TODO: [HANDLE] (ProjectService.UpdateProject) Handle this return(false); } var projectEntity = projectDto.AsProjectEntity(); projectEntity.UserId = userId; using (builder.WithCustomTiming2()) { builder.IncrementQueryCount(); if (await _projectRepo.Update(projectEntity) <= 0) { return(false); } builder.IncrementResultsCount(); return(true); } } } catch (Exception ex) { _logger.LogUnexpectedException(ex); builder.WithException(ex); return(false); } finally { await _metrics.SubmitPointAsync(builder.Build()); } }