Beispiel #1
0
        public async Task <Revendedor> Register(Revendedor entity)
        {
            const string methodName = nameof(Register);
            string       header     = $"METHOD | {entity.Email} | {serviceName}: {methodName}";

            try
            {
                logger.LogInformation((int)LogEventEnum.Events.InsertItem,
                                      $"{header} - {MessageLog.Saving.Value}");

                if (string.IsNullOrEmpty(entity.Senha))
                {
                    logger.LogWarning((int)LogEventEnum.Events.GetItemNotFound,
                                      $"{header} - {MessageError.PasswordNullorEmpty.Value}");

                    throw new Exception(MessageError.PasswordNullorEmpty.Value);
                }

                entity.Senha = HashOptions.CreatePasswordHash(entity.Senha);

                entity.Cpf = entity.Cpf.Replace(".", string.Empty).Replace("-", string.Empty)
                             .Replace(";", string.Empty).Replace(",", string.Empty).Replace("/", string.Empty);

                context.Revendedores.Add(entity);

                await context.SaveChangesAsync();

                logger.LogInformation((int)LogEventEnum.Events.InsertItem,
                                      $"{header} - {MessageLog.Saved.Value} | ID: {entity.Id}");

                string json = JsonConvert.SerializeObject(entity);

                await historicoService.Create(new Historico
                {
                    ChaveTabela     = entity.Id,
                    NomeTabela      = typeof(Revendedor).Name,
                    JsonAntes       = string.Empty,
                    JsonDepois      = json,
                    Usuario         = entity.Email,
                    IdTipoHistorico = (int)TipoHistoricoEnum.Action.Register
                });

                return(entity);
            }
            catch (Exception)
            {
                throw;
            }
        }
Beispiel #2
0
        public async Task <StatusCompra> Create(StatusCompra entity, string usuario)
        {
            const string methodName = nameof(Create);
            string       header     = $"METHOD | {usuario} | {serviceName}: {methodName}";

            try
            {
                logger.LogInformation((int)LogEventEnum.Events.InsertItem,
                                      $"{header} - {MessageLog.Saving.Value}");

                context.StatusCompra.Add(entity);

                await context.SaveChangesAsync();

                logger.LogInformation((int)LogEventEnum.Events.InsertItem,
                                      $"{header} - {MessageLog.Saved.Value} - ID: {entity.Id}");

                string json = JsonConvert.SerializeObject(entity);

                await historicoService.Create(new Historico
                {
                    ChaveTabela     = entity.Id,
                    NomeTabela      = typeof(StatusCompra).Name,
                    JsonAntes       = string.Empty,
                    JsonDepois      = json,
                    Usuario         = usuario,
                    IdTipoHistorico = (int)TipoHistoricoEnum.Action.Create
                });

                return(entity);
            }
            catch (Exception)
            {
                throw;
            }
        }
Beispiel #3
0
        public async Task <Compra> Create(Compra entity, string usuario)
        {
            const string methodName = nameof(Create);
            string       header     = $"METHOD | {usuario} | {serviceName}: {methodName}";

            try
            {
                logger.LogInformation((int)LogEventEnum.Events.GetItem,
                                      $"{header} - {MessageLog.Getting.Value} - Buscando {nameof(Revendedor)} por E-Mail: {usuario}");

                Revendedor revendedor = await context.Revendedores.AsNoTracking()
                                        .SingleOrDefaultAsync(item => item.Email.Equals(usuario));

                if (revendedor is null)
                {
                    logger.LogWarning((int)LogEventEnum.Events.GetItemNotFound,
                                      $"{header} - {MessageError.NotFoundSingle.Value} E-Mail de busca: {usuario}");

                    return(null);
                }

                logger.LogInformation((int)LogEventEnum.Events.GetItem,
                                      $"{header} - {MessageLog.Getted.Value} - ID Revendedor: {revendedor.Id}");

                entity.IdRevendedor = revendedor.Id;

                logger.LogInformation((int)LogEventEnum.Events.GetItem,
                                      $"{header} - {MessageLog.Getting.Value} - Buscando Status pelo CPF: {entity.CpfRevendedor}");

                entity.IdStatus = await regrasCompra.GetStatusCompraId(entity.CpfRevendedor, usuario);

                logger.LogInformation((int)LogEventEnum.Events.GetItem,
                                      $"{header} - {MessageLog.Getted.Value} - ID Status: {entity.Status}");

                logger.LogInformation((int)LogEventEnum.Events.GetItem,
                                      $"{header} - {MessageLog.Getting.Value} - Buscando Regra de Percentual de Cashback pelo Valor: {entity.Valor}");

                entity.PercentualCashback = await regrasCompra.GetPercentualCashback(entity.Valor, usuario);

                logger.LogInformation((int)LogEventEnum.Events.GetItem,
                                      $"{header} - {MessageLog.Getted.Value} - Percentual Cashback: {entity.PercentualCashback}");

                logger.LogInformation((int)LogEventEnum.Events.GetItem,
                                      $"{header} - {MessageLog.Getting.Value} - Realizando calculo do Valor de Cashback ({entity.PercentualCashback} / 100) * {entity.Valor}");

                entity.ValorCashback = (entity.PercentualCashback / 100) * entity.Valor;

                logger.LogInformation((int)LogEventEnum.Events.GetItem,
                                      $"{header} - {MessageLog.Getted.Value} - Valor Cashback: {entity.ValorCashback}");

                logger.LogInformation((int)LogEventEnum.Events.InsertItem,
                                      $"{header} - {MessageLog.Saving.Value}");

                context.Compras.Add(entity);

                await context.SaveChangesAsync();

                logger.LogInformation((int)LogEventEnum.Events.InsertItem,
                                      $"{header} - {MessageLog.Saved.Value} - ID: {entity.Id}");

                string json = JsonConvert.SerializeObject(entity);

                await historicoService.Create(new Historico
                {
                    ChaveTabela     = entity.Id,
                    NomeTabela      = typeof(Compra).Name,
                    JsonAntes       = string.Empty,
                    JsonDepois      = json,
                    Usuario         = usuario,
                    IdTipoHistorico = (int)TipoHistoricoEnum.Action.Create
                });

                return(entity);
            }
            catch (Exception)
            {
                throw;
            }
        }