Пример #1
0
 public SaveVesselScheduleResponse SaveVesselSchedule(SaveVesselScheduleRequest request)
 {
     try
     {
         if (request.Id == 0)
         {
             var vesselSchedule = request.MapTo<VesselSchedule>();
             var buyer = DataContext.Buyers.First(x => x.Id == request.BuyerId);// new Buyer { Id = request.BuyerId };
             //DataContext.Buyers.Attach(buyer);
             var vessel = DataContext.Vessels.First(x => x.Id == request.VesselId);// new Vessel { Id = request.VesselId };
             //DataContext.Vessels.Attach(vessel);
             vesselSchedule.Name = vessel.Name + "-" + buyer.Name + "-" + vessel.Type + "-" + vesselSchedule.Cargo;
             vesselSchedule.Buyer = buyer;
             vesselSchedule.Vessel = vessel;
             DataContext.VesselSchedules.Add(vesselSchedule);
         }
         else
         {
             var vesselSchedule = DataContext.VesselSchedules.FirstOrDefault(x => x.Id == request.Id);
             if (vesselSchedule != null)
             {
                 request.MapPropertiesToInstance<VesselSchedule>(vesselSchedule);
                 var buyer = DataContext.Buyers.First(x => x.Id == request.BuyerId);// new Buyer { Id = request.BuyerId };
                 //DataContext.Buyers.Attach(buyer);
                 var vessel = DataContext.Vessels.First(x => x.Id == request.VesselId);// new Vessel { Id = request.VesselId };
                 //DataContext.Vessels.Attach(vessel);
                 vesselSchedule.Name = vessel.Name + "-" + buyer.Name + "-" + vessel.Type + "-" + vesselSchedule.Cargo;
                 vesselSchedule.Buyer = buyer;
                 vesselSchedule.Vessel = vessel;
             }
         }
         DataContext.SaveChanges();
         return new SaveVesselScheduleResponse
         {
             IsSuccess = true,
             Message = "Vessel Schedule has been saved"
         };
     }
     catch (InvalidOperationException e)
     {
         return new SaveVesselScheduleResponse
         {
             IsSuccess = false,
             Message = e.Message
         };
     }
 }
Пример #2
0
        public SaveVesselScheduleResponse SaveVesselSchedule(SaveVesselScheduleRequest request)
        {
            try
            {
                var vesselSchedule = request.MapTo<VesselSchedule>();
                if (request.Id == 0)
                {

                    var buyer = DataContext.Buyers.First(x => x.Id == request.BuyerId);// new Buyer { Id = request.BuyerId };
                    //DataContext.Buyers.Attach(buyer);
                    var vessel = DataContext.Vessels.First(x => x.Id == request.VesselId);// new Vessel { Id = request.VesselId };
                    //DataContext.Vessels.Attach(vessel);
                    vesselSchedule.Name = string.Format("{0}-{1}-{2}-{3}", vessel.Name, buyer.Name, vessel.Type, vesselSchedule.Cargo);
                    vesselSchedule.Buyer = buyer;
                    vesselSchedule.Vessel = vessel;
                    DataContext.VesselSchedules.Add(vesselSchedule);
                }
                else
                {
                    vesselSchedule = DataContext.VesselSchedules.FirstOrDefault(x => x.Id == request.Id);
                    if (vesselSchedule != null)
                    {
                        request.MapPropertiesToInstance<VesselSchedule>(vesselSchedule);
                        var buyer = DataContext.Buyers.First(x => x.Id == request.BuyerId);// new Buyer { Id = request.BuyerId };
                        //DataContext.Buyers.Attach(buyer);
                        var vessel = DataContext.Vessels.First(x => x.Id == request.VesselId);// new Vessel { Id = request.VesselId };
                        //DataContext.Vessels.Attach(vessel);
                        vesselSchedule.Name = string.Format("{0}-{1}-{2}-{3}", vessel.Name, buyer.Name, vessel.Type, vesselSchedule.Cargo);
                        vesselSchedule.Buyer = buyer;
                        vesselSchedule.Vessel = vessel;
                    }
                }
                DataContext.SaveChanges();
                var response = new SaveVesselScheduleResponse
                {
                    IsSuccess = true,
                    Message = "Vessel Schedule has been saved"
                };
                vesselSchedule = DataContext.VesselSchedules.Include(x => x.Buyer)
                    .Include(x => x.Vessel)
                    .Include(x => x.Vessel.Measurement)
                    .Single(x => x.Id == vesselSchedule.Id);
                vesselSchedule.MapPropertiesToInstance(response);
                if (request.DerTransactionDate.HasValue) {
                    var latestRemark = DataContext.NextLoadingSchedules.Where(x => x.VesselSchedule.Id == vesselSchedule.Id && x.CreatedAt <= request.DerTransactionDate)
                   .OrderByDescending(x => x.CreatedAt)
                   .FirstOrDefault();
                    if (latestRemark != null)
                    {
                        response.RemarkDate = latestRemark.CreatedAt.ToString("dd-MM-yyyy");
                        response.Remark = latestRemark.Remark;
                    }
                }

                return response;
            }
            catch (InvalidOperationException e)
            {
                return new SaveVesselScheduleResponse
                {
                    IsSuccess = false,
                    Message = e.Message
                };
            }
        }