示例#1
0
        public async Task <ActionResult <AgencyDto> > CreateAgency(AgencyForCreationDto agencyForCreationDto)
        {
            var agencyEntity = _mapper.Map <Agency>(agencyForCreationDto);

            //add payment record
            var receiptType = await _systemRepo.GetReceiptType(agencyForCreationDto.ReceiptTypeId);

            if (receiptType == null)
            {
                return(BadRequest("Could not find receiptType."));
            }

            agencyEntity.Payment = new Payment {
                AmountBalance        = 0,
                CurrentReceiptNumber = 0,
                MinUnitThreshold     = (agencyForCreationDto.MinUnitThreshold > 0 ? agencyForCreationDto.MinUnitThreshold : 10),
                Unit               = 0,
                ReceiptType        = receiptType.Type,
                UnitPerReceiptType = receiptType.UnitPerType
            };

            _AgenciesRepo.Add(agencyEntity);

            if (await _AgenciesRepo.Save())
            {
                var AgencyToReturn = _mapper.Map <AgencyDto>(agencyEntity);
                return(CreatedAtRoute("GetAgency", new { AgencyToReturn.Id }, AgencyToReturn));
            }

            throw new Exception("Creating Agency failed on save");
        }
示例#2
0
        public GeneralResponse EditAgency(EditAgencyRequest request, Guid modifiedEmployeeID)
        {
            GeneralResponse response = new GeneralResponse();

            try
            {
                Agency agency = new Agency();
                agency = _agencyRepository.FindBy(request.ID);

                if (agency != null)
                {
                    agency.ModifiedDate = PersianDateTime.Now;

                    agency.ModifiedEmployee = _employeeRepository.FindBy(modifiedEmployeeID);
                    if (request.AgencyName != null)
                    {
                        agency.AgencyName = request.AgencyName;
                    }
                    if (request.ManagerName != null)
                    {
                        agency.ManagerName = request.ManagerName;
                    }
                    if (request.Phone1 != null)
                    {
                        agency.Phone1 = request.Phone1;
                    }
                    if (request.Phone2 != null)
                    {
                        agency.Phone2 = request.Phone2;
                    }
                    if (request.Mobile != null)
                    {
                        agency.Mobile = request.Mobile;
                    }
                    if (request.Address != null)
                    {
                        agency.Address = request.Address;
                    }
                    if (request.Note != null)
                    {
                        agency.Note = request.Note;
                    }

                    agency.Discontinued = request.Discontinued;

                    #region RowVersion - Validation
                    if (agency.RowVersion != request.RowVersion)
                    {
                        response.ErrorMessages.Add("EditConcurrencyKey");
                        return(response);
                    }
                    else
                    {
                        agency.RowVersion += 1;
                    }

                    if (agency.GetBrokenRules().Count() > 0)
                    {
                        foreach (BusinessRule businessRule in agency.GetBrokenRules())
                        {
                            response.ErrorMessages.Add(businessRule.Rule);
                        }

                        return(response);
                    }

                    #endregion

                    _agencyRepository.Save(agency);
                }
                else
                {
                    response.ErrorMessages.Add("NoItemToEditKey");
                }
                _uow.Commit();
            }
            catch (Exception ex)
            {
                response.ErrorMessages.Add(ex.Message);
                if (ex.InnerException != null)
                {
                    response.ErrorMessages.Add(ex.InnerException.Message);
                }
            }

            return(response);
        }