Example #1
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());
            }
        }