Exemple #1
0
 public static Drive Map(Data.ArchiveDrive drive)
 {
     if (drive == null)
     {
         return(null);
     }
     if (drive.Dtype == "Pickup")//Pickup
     {
         var d = new PickupDrive(Map(drive.Driver), Map(drive.Destination), drive.Dtime.Value, drive.Id);
         foreach (Data.ArchiveOrder pickup in drive.ArchiveOrder)//adds users and items
         {
             User u = Map(pickup.User);
             d.AddUser(u);
             foreach (Data.ArchiveItem item in pickup.ArchiveItem)
             {
                 d.AddItem(u, Map(item));
             }
         }
         return(d);
     }
     else//Join
     {
         var d = new JoinDrive(Map(drive.Driver), Map(drive.Destination), drive.Dtime.Value, drive.Id);
         foreach (Data.ArchiveUserJoin join in drive.ArchiveUserJoin)//adds users
         {
             d.AddUser(Map(join.User));
         }
         return(d);
     }
 }
        /// <summary>
        /// Adds a new active drive. Throws an exception if wrong parameters
        /// </summary>
        /// <param name="driverId"></param>
        /// <param name="destId"></param>
        /// <param name="time"></param>
        /// <param name="isJoin"></param>
        /// <returns></returns>
        public async Task <Drive> NewDrive(int driverId, int destId, DateTime time, bool isPickup)
        {
            Driver driver = await GetDriverByDriverId(driverId);

            Destination dest = await GetDestinationById(destId);

            if (driver == null || dest == null || time < DateTime.Now.AddMinutes(Drive.Buffer))
            {
                throw new Exception("Improper drive parameters.");
            }

            IEnumerable <Drive> drives = await GetDrivesByDriver(driverId);

            Drive lastDrive = drives.Last();

            //checks to see if the driver created a drive in the past 12 hours
            if (time < DateTime.Now.AddHours(12))
            {
                throw new Exception("You must wait 12 hours to create a new drive");
            }

            Drive d;

            if (!isPickup)
            {
                d = new JoinDrive(driver, dest, time);
            }
            else
            {
                d = new PickupDrive(driver, dest, time);
            }
            try
            {
                bbBContext.Add(Mapper.MapActive(d));
                await bbBContext.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                logger.Info(ex);
                throw;
            }
            return(d);
        }