Ejemplo n.º 1
0
        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());
            }
        }
Ejemplo n.º 2
0
        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());
            }
        }
Ejemplo n.º 3
0
        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());
            }
        }
Ejemplo n.º 4
0
        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());
            }
        }
Ejemplo n.º 5
0
        // 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());
            }
        }
Ejemplo n.º 6
0
        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());
            }
        }