public IHttpActionResult SavePostCode(FraytePostCodeUK _postcode)
 {
     new ZonePostCodeRepository().SaveZonePostCode(_postcode);
     return(Ok());
 }
示例#2
0
 public void SaveZonePostCode(FraytePostCodeUK _postcode)
 {
     try
     {
         LogisticServiceZonePostCode zpc;
         if (_postcode != null)
         {
             var result = dbContext.LogisticServiceZones.Where(a => a.LogisticServiceZoneId == _postcode.LogisticServiceZoneId).FirstOrDefault();
             if (result != null)
             {
                 result.TransitTime            = _postcode.TransitTime;
                 dbContext.Entry(result).State = System.Data.Entity.EntityState.Modified;
                 dbContext.SaveChanges();
             }
             if (_postcode.FrayteZonePostCodeList != null && _postcode.FrayteZonePostCodeList.Count > 0)
             {
                 foreach (var code in _postcode.FrayteZonePostCodeList)
                 {
                     if (code.IsActive)
                     {
                         zpc = dbContext.LogisticServiceZonePostCodes.Where(x => x.LogisticServiceZonePostCodeId == code.ZonePostCodeId).FirstOrDefault();
                         if (zpc != null)
                         {
                             zpc = new LogisticServiceZonePostCode();
                             if (code.LogisticCompany == FrayteLogisticServiceType.DHL)
                             {
                                 zpc.DHLLogisticServiceZoneId = code.PostCodeZone.ZoneId;
                             }
                             else if (code.LogisticCompany == FrayteLogisticServiceType.UkMail)
                             {
                                 zpc.UKMailLogisticServiceZoneId = code.PostCodeZone.ZoneId;
                             }
                             else if (code.LogisticCompany == FrayteLogisticServiceType.Hermes)
                             {
                                 zpc.HermesLogisticServiceZoneId = code.PostCodeZone.ZoneId;
                             }
                             else if (code.LogisticCompany == FrayteLogisticServiceType.Yodel)
                             {
                                 zpc.YodelLogisticServiceZoneId = code.PostCodeZone.ZoneId;
                             }
                             zpc.PostCode = code.PostCode;
                             dbContext.Entry(code).State = System.Data.Entity.EntityState.Modified;
                             dbContext.SaveChanges();
                         }
                         else
                         {
                             zpc = new LogisticServiceZonePostCode();
                             if (code.LogisticCompany == FrayteLogisticServiceType.DHL)
                             {
                                 zpc.DHLLogisticServiceZoneId = code.PostCodeZone.ZoneId;
                             }
                             else if (code.LogisticCompany == FrayteLogisticServiceType.UkMail)
                             {
                                 zpc.UKMailLogisticServiceZoneId = code.PostCodeZone.ZoneId;
                             }
                             else if (code.LogisticCompany == FrayteLogisticServiceType.Hermes)
                             {
                                 zpc.HermesLogisticServiceZoneId = code.PostCodeZone.ZoneId;
                             }
                             else if (code.LogisticCompany == FrayteLogisticServiceType.Yodel)
                             {
                                 zpc.YodelLogisticServiceZoneId = code.PostCodeZone.ZoneId;
                             }
                             zpc.PostCode = code.PostCode;
                             dbContext.LogisticServiceZonePostCodes.Add(zpc);
                             dbContext.SaveChanges();
                         }
                     }
                     else
                     {
                         zpc = dbContext.LogisticServiceZonePostCodes.Where(p => p.PostCode == code.PostCode).FirstOrDefault();
                         if (zpc != null)
                         {
                             dbContext.LogisticServiceZonePostCodes.Remove(zpc);
                             dbContext.SaveChanges();
                         }
                     }
                 }
             }
         }
     }
     catch (DbEntityValidationException ex)
     {
         foreach (var entityValidationErrors in ex.EntityValidationErrors)
         {
             foreach (var validationError in entityValidationErrors.ValidationErrors)
             {
                 string ss = "Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage;
             }
         }
     }
 }