Exemplo n.º 1
0
        public IHttpActionResult UpdateAgreement(int id, [FromBody] BaseRate.RateType newBaseRateType)
        {
            Agreement agreement = db.Agreements.Include("Customer").SingleOrDefault(x => x.Id == id);

            if (agreement == null)
            {
                return(NotFound());
            }

            BaseRate.RateType oldBaseRateType = agreement.BaseRateType;
            agreement.BaseRateType = newBaseRateType;

            db.MarkAsModified(agreement);

            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }

            // check if base rate record exists

            BaseRate currentBaseRate = db.BaseRates.SingleOrDefault(x => x.Code == oldBaseRateType);
            BaseRate newBaseRate     = db.BaseRates.SingleOrDefault(x => x.Code == newBaseRateType);

            if (currentBaseRate == null || newBaseRate == null)
            {
                // Better solution would be to throw a custom exception with an error message

                return(InternalServerError(new ArgumentNullException("Base Rate record was not found")));
            }

            // to client we need to return an agreement with an old base rate type
            agreement.BaseRateType = oldBaseRateType;

            var agremeentExtended = Agreements.GetExtendedAgreement(agreement, newBaseRateType, currentBaseRate.Value, newBaseRate.Value);

            return(Ok(agremeentExtended));
        }