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 <ClientDto> GetById(int userId, int clientId) { // TODO: [TESTS] (ClientService.GetById) Add tests var builder = new ServiceMetricBuilder(nameof(ClientService), nameof(GetById)) .WithCategory(MetricCategory.Client, MetricSubCategory.GetById) .WithCustomInt1(userId) .WithCustomInt2(clientId); try { using (builder.WithTiming()) { ClientEntity dbClient; using (builder.WithCustomTiming2()) { builder.IncrementQueryCount(); dbClient = await _clientRepo.GetById(clientId); builder.CountResult(dbClient); } if (dbClient == null) { return(null); } // ReSharper disable once InvertIf if (dbClient.UserId != userId) { // TODO: [HANDLE] (ClientService.GetById) Handle this better _logger.Warning("Requested client '{cname}' ({cid}) does not belong to user ({uid})", dbClient.ClientName, dbClient.ClientId, userId ); return(null); } return(ClientDto.FromEntity(dbClient)); } } 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> 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()); } }