Ejemplo n.º 1
0
 private Contract Create(int serviceSupplierId, Models.Requests.Contract contract)
 => new Contract
 {
     Name              = contract.Name,
     Description       = contract.Description,
     ValidFrom         = contract.ValidFrom.Date,
     ValidTo           = contract.ValidTo.Date,
     Modified          = _dateTimeProvider.UtcNow(),
     ServiceSupplierId = serviceSupplierId
 };
Ejemplo n.º 2
0
        public Task <Result <Models.Responses.Contract> > Add(Models.Requests.Contract contract)
        {
            return(_managerContext.GetServiceSupplier()
                   .Check(serviceSupplier => _serviceSupplierContext.EnsureAccommodationBelongsToServiceSupplier(serviceSupplier, contract.AccommodationId))
                   .Bind(serviceSupplier =>
            {
                var validationResult = ValidationHelper.Validate(contract, new ContractValidator());

                return validationResult.IsFailure
                        ? Result.Failure <ServiceSupplier>(validationResult.Error)
                        : Result.Success(serviceSupplier);
            })
                   .Map(serviceSupplier => Create(serviceSupplier.Id, contract))
                   .Map(Add)
                   .Map(dbContract => Build(dbContract, contract.AccommodationId)));


            async Task <Contract> Add(Contract dbContract)
            {
                dbContract.Created = _dateTimeProvider.UtcNow();
                var entry = _dbContext.Contracts.Add(dbContract);
                await _dbContext.SaveChangesAsync();

                _dbContext.DetachEntry(entry.Entity);

                var contractAccommodationRelationEntry = _dbContext.ContractAccommodationRelations.Add(
                    new ContractAccommodationRelation
                {
                    ContractId = entry.Entity.Id, AccommodationId = contract.AccommodationId
                });
                await _dbContext.SaveChangesAsync();

                _dbContext.DetachEntry(contractAccommodationRelationEntry.Entity);

                return(entry.Entity);
            }
        }
Ejemplo n.º 3
0
        public async Task <Result> Update(int contractId, Models.Requests.Contract contract)
        {
            return(await _managerContext.GetServiceSupplier()
                   .Check(serviceSupplier => _serviceSupplierContext.EnsureContractBelongsToServiceSupplier(serviceSupplier, contractId))
                   .Check(serviceSupplier => _serviceSupplierContext.EnsureAccommodationBelongsToServiceSupplier(serviceSupplier, contract.AccommodationId))
                   .Bind(serviceSupplier =>
            {
                var(_, failure, error) = ValidationHelper.Validate(contract, new ContractValidator());

                return failure ? Result.Failure <ServiceSupplier>(error) : Result.Success(serviceSupplier);
            })
                   .Map(serviceSupplier => Create(serviceSupplier.Id, contract))
                   .Tap(Update));


            async Task Update(Contract dbContract)
            {
                dbContract.Id = contractId;
                var entry = _dbContext.Contracts.Update(dbContract);
                await _dbContext.SaveChangesAsync();

                _dbContext.DetachEntry(entry.Entity);
            }
        }