Пример #1
0
        public async Task <ResponseService> Handle(ChangeBarberShopStatusCommand command, CancellationToken cancellationToken)
        {
            try
            {
                var plan = await _repositoryPlan.GetAsync(command.PlanId);

                if (plan == null)
                {
                    return(new ResponseService(messageError: "This plan does not exists"));
                }

                int userId     = _identity.GetUserIdentity();
                var barberShop = await _repositoryBarberShop.GetAsync(command.BarberShopId);

                if (barberShop.UserAdminId == userId)
                {
                    var planType = await _repositoryPlanType.GetAsync(plan.PlanTypeId);

                    var barberShopStatus = await _repositoryBarberShopStatus.GetAsync(barberShop.BarberShopStatusId);

                    var expiration = barberShopStatus.ExpirationDate.AddDays(planType.Days);

                    barberShopStatus = new Domain.Entities.BarberShopStatus
                    {
                        PlanId         = plan.Id.GetValueOrDefault(),
                        ExpirationDate = expiration,
                        UpdateAt       = DateTime.UtcNow
                    };

                    using (TransactionScope transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                    {
                        bool result = await _repositoryBarberShopStatus.UpdateAsync(barberShopStatus);

                        if (result)
                        {
                            return(new ResponseService(result));
                        }
                        else
                        {
                            return(new ResponseService(messageError: $"Error updating BarberShopStatus: {barberShopStatus.Id}"));
                        }
                    }
                }
                else
                {
                    return(new ResponseService(messageError: "User isn't admin"));
                }
            }
            catch (Exception ex)
            {
                return(new ResponseService(messageError: ex.Message));
            }
        }
Пример #2
0
        public async Task <ResponseService> Handle(RemoveBarberShopCommand command, CancellationToken cancellationToken)
        {
            try
            {
                cancellationToken.ThrowIfCancellationRequested();

                var barberShop = await _repositoryBarberShop.GetAsync(command.Id);

                var address          = new Domain.Entities.Address(barberShop.AddressId);
                var barberShopStatus = new Domain.Entities.BarberShopStatus(barberShop.BarberShopStatusId);

                using (TransactionScope transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    bool result;

                    if (!string.IsNullOrEmpty(barberShop.PathLogo))
                    {
                        _imageService.RemoveImage(barberShop.PathLogo);
                    }

                    result = await _repositoryAddress.DeleteAsync(address);

                    if (result)
                    {
                        result = await _repositoryBarberShopStatus.DeleteAsync(barberShopStatus);
                    }
                    if (result)
                    {
                        result = await _repositoryBarberShop.DeleteAsync(barberShop);
                    }

                    if (result)
                    {
                        transaction.Complete();
                        return(new ResponseService(result));
                    }
                    else
                    {
                        return(new ResponseService(messageError: $"Error deleting BarberShop: {barberShop.Name}"));
                    }
                }
            }
            catch (Exception ex)
            {
                return(new ResponseService(messageError: ex.Message));
            }
        }
Пример #3
0
        public async Task <ResponseService> Handle(CreateBarberShopCommand command, CancellationToken cancellationToken)
        {
            try
            {
                cancellationToken.ThrowIfCancellationRequested();

                var address = new Domain.Entities.Address
                {
                    PublicPlace  = command.PublicPlace,
                    Number       = command.Number,
                    Neighborhood = command.Neighborhood,
                    Locality     = command.Locality,
                    State        = command.State,
                    Latitude     = command.Latitude,
                    Longitude    = command.Longitude
                };

                var barberShopStatus = new Domain.Entities.BarberShopStatus(DateTime.UtcNow);

                var barberShop = new Domain.Entities.BarberShop
                {
                    IsActive    = false,
                    Name        = command.Name,
                    UserAdminId = _identity.GetUserIdentity()
                };

                using (TransactionScope transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    if (!string.IsNullOrEmpty(command.LogoBase64.Trim()))
                    {
                        string nameFile = Guid.NewGuid().ToString();
                        barberShop.PathLogo = nameFile;

                        _imageService.SaveImage(command.LogoBase64, nameFile);
                    }

                    int?addressId = await _repositoryAddress.CreateAsync(address);

                    int?barberShopStatusId = await _repositoryBarberShopStatus.CreateAsync(barberShopStatus);

                    barberShop.AddressId          = addressId.GetValueOrDefault();
                    barberShop.BarberShopStatusId = barberShopStatusId.GetValueOrDefault();

                    int?barberShopId = await _repositoryBarberShop.CreateAsync(barberShop);

                    if (barberShopId > 0)
                    {
                        transaction.Complete();
                        return(new ResponseService(barberShopId));
                    }
                    else
                    {
                        return(new ResponseService(messageError: $"Error inserting BarberShop: {barberShop.Name}"));
                    }
                }
            }
            catch (Exception ex)
            {
                return(new ResponseService(messageError: ex.Message));
            }

            // CreateBarberShopNotification addBarberShopNotification = new CreateBarberShopNotification(barberShop);
            // await _mediator.Publish(addBarberShopNotification);
        }