Пример #1
0
        public DTO.Store GetByStore(Guid storeCode, Guid clientId)
        {
            if (storeCode.IsEmpty())
            {
                throw new ArgumentException("Código da loja inválido!");
            }

            Store store = null;

            var applicationStore = applicationStoreRepository.GetByClientId(clientId);

            if (!applicationStore.Store.IsMain && storeCode != applicationStore.StoreCode)
            {
                throw new ArgumentException("Não é possível efetuar essa operação");
            }

            store = storeRepository.Get(storeCode, includeApplicationStore: true);

            if (store.IsNull())
            {
                throw new ArgumentException("Loja não encontrada");
            }

            var dto = new DTO.Store()
            {
                Code         = store.Code,
                Applications = store.ApplicationsStore.Select(a => new DTO.ApplicationStore(a, false)).ToList()
            };

            return(dto);
        }
Пример #2
0
        public IEnumerable <DTO.ApplicationStore> ConnectApplications(DTO.Store store)
        {
            var applicationStoreCollection = new List <DTO.ApplicationStore>();

            var _store = storeRepository.Get(store.Code);

            if (_store.IsNull())
            {
                throw new ArgumentException("Ops! Esta loja não possui cadastro. :(");
            }

            using (var transaction = Connection.BeginTransaction())
            {
                try
                {
                    store.Applications.ForEach(application =>
                    {
                        var _application = applicationRepository.Get(application.ApplicationName);

                        if (_application.IsNull())
                        {
                            throw new ArgumentException("Ops! A aplicação '{0}' não existe. :(".ToFormat(application.ApplicationName));
                        }

                        var applicationStore = applicationStoreRepository.Get(_application.Code.Value, _store.Code);

                        if (applicationStore.IsNull())
                        {
                            applicationStoreCollection.Add(new DTO.ApplicationStore(applicationStoreService.Save(_application, _store, application.AllowedOrigins)));
                        }
                        else
                        {
                            if (!applicationStore.Status)
                            {
                                applicationStore.Status = true;
                                applicationStoreRepository.Update(applicationStore);
                            }

                            applicationStoreCollection.Add(new DTO.ApplicationStore(applicationStore));
                        }
                    });

                    transaction.Commit();
                }
                catch
                {
                    if (!transaction.IsNull())
                    {
                        transaction.Rollback();
                    }

                    throw;
                }
            }

            return(applicationStoreCollection);
        }
Пример #3
0
 public IHttpActionResult ConnectApplications(DTO.Store store)
 {
     try
     {
         return(Ok(storeApp.ConnectApplications(store)));
     }
     catch (ArgumentException ex)
     {
         return(BadRequest(ex));
     }
     catch (Exception ex)
     {
         return(InternalServerError(ex));
     }
 }
Пример #4
0
 public IHttpActionResult SaveStore(DTO.Store store)
 {
     try
     {
         return(Ok(storeApp.Save(store)));
     }
     catch (ArgumentException ex)
     {
         return(BadRequest(ex));
     }
     catch (Exception ex)
     {
         return(InternalServerError(ex));
     }
 }
Пример #5
0
        public IEnumerable <DTO.ApplicationStore> Save(DTO.Store store)
        {
            if (store.IsNull())
            {
                throw new ArgumentException("Informe os dados da loja.");
            }

            var domainStore = new Store(store);

            domainStore.IsValid();

            Store newStore = Create(domainStore);

            return(newStore.ApplicationsStore.Select(a => new DTO.ApplicationStore(a)));
        }
Пример #6
0
        public DTO.Store Update(DTO.Store store)
        {
            Store result;

            if (store.Code.IsEmpty())
            {
                throw new ArgumentException("Ops! É necessário enviar o código da loja. :(");
            }

            if (!store.Cnpj.IsNullOrWhiteSpace() && ((store.Cnpj.Length <= 11 && !store.Cnpj.IsValidCPF()) || (store.Cnpj.Length > 11 && !store.Cnpj.IsValidCNPJ())))
            {
                throw new ArgumentException("Ops, CNPJ inválido");
            }

            if (store.Name.IsNullOrWhiteSpace())
            {
                throw new ArgumentException("Ops, Nome de loja inválido");
            }

            using (var transaction = Connection.BeginTransaction())
            {
                try
                {
                    var updateStore = storeRepository.Get(store.Code);

                    if (updateStore.IsNull())
                    {
                        throw new ArgumentException("Ops, loja não encontrada");
                    }

                    updateStore.Name       = store.Name;
                    updateStore.Cnpj       = store.Cnpj;
                    updateStore.UpdateDate = DateTime.Now;

                    result = storeRepository.Save(updateStore);

                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }

            return(new DTO.Store(result));
        }