public async Task <StoreDto> Handle(CreateStoreCommand command, CancellationToken cancellationToken)
        {
            var store = new Store.Domain.Store {
                Name = command.Name, Location = command.Location
            };
            await _storeRepository.AddAsync(store, cancellationToken);

            return(_mapper.Map <StoreDto>(store));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> PostAsync([FromBody] Store store)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.GetErrorMessages()));
            }

            try
            {
                await _storeRepo.AddAsync(store);

                await _storeRepo.SaveChangeAsync();

                var returnedResource = Mapper.Map <Store, StoreReturnResource>(store);

                return(Ok(returnedResource));
            }
            catch (Exception ex)
            {
                return(BadRequest(new List <string> {
                    ex.Message
                }));
            }
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Post([FromBody] AddStoreViewModel model)
        {
            if (model == null)
            {
                return(BadRequest());
            }

            Guid?accountId = null;

            if (model.AccountId.HasValue)
            {
                var account = await _accountRepo.GetAsync(model.AccountId.Value);

                if (account == null)
                {
                    return(NotFound(Resources.Accounts.AccountResource.AccountNotFound));
                }
                accountId = account.Id;
            }

            Guid?costCenterId = null;

            if (model.CostCenterId.HasValue)
            {
                var costCenter = await _costCenterRepo.GetAsync(model.CostCenterId.Value);

                if (costCenter == null)
                {
                    return(NotFound(Resources.CostCenters.CostCenterResource.CostCenterNotFound));
                }
                costCenterId = costCenter.Id;
            }

            Guid?parentId = null;

            if (model.ParentId.HasValue)
            {
                var parentStore = await _storeRepo.GetAsync(model.ParentId.Value);

                if (parentStore == null)
                {
                    return(NotFound(Resources.Stores.StoreResource.ParentStoreNotFound));
                }
                parentId = parentStore.Id;
            }

            if (await _storeRepo.IsExistCodeAsync(model.Code))
            {
                ModelState.AddModelError("Code", Resources.Global.Common.ThisCodeExist);
            }
            if (await _storeRepo.IsExistNameAsync(model.Name))
            {
                ModelState.AddModelError("Name", Resources.Global.Common.ThisNameExist);
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.GetWithErrorsKey()));
            }

            var store = new Store(model.Name, model.Code, accountId, costCenterId, model.Note);

            if (parentId.HasValue)
            {
                store.ParentId = parentId.Value;
            }

            var affectedRows = await _storeRepo.AddAsync(store);

            if (affectedRows > 0)
            {
                _storeRepo.LoadReferences(store);

                var viewModel = AutoMapper.Mapper.Map <StoreViewModel>(store);

                return(CreatedAtRoute("GetStore", new { id = store.Number }, viewModel));
            }
            return(BadRequest());
        }