예제 #1
0
        public ActionResult Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                User user = new User();
                user.FirstName = model.FirstName;
                user.LastName  = model.LastName;
                user.Email     = model.Email;
                user.Password  = model.Password;

                CarpoolContext context = new CarpoolContext();
                context.Users.Add(user);
                try {
                    context.SaveChanges();
                }catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) {
                    Exception raise = dbEx;
                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        {
                            string message = string.Format("{0}:{1}",
                                                           validationErrors.Entry.Entity.ToString(),
                                                           validationError.ErrorMessage);
                            // raise a new exception nesting
                            // the current instance as InnerException
                            raise = new InvalidOperationException(message, raise);
                        }
                    }
                    throw raise;
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
예제 #2
0
 public string RequestRide(ApplicationUser user, RideRquestModel model)
 {
     try
     {
         Ride      ride      = carpoolDb.Rides.FirstOrDefault(c => c.Id.Equals(model.RideId));
         Rider     rider     = carpoolDb.Riders.FirstOrDefault(c => c.ApplicationUserId.Equals(user.Id));
         Driver    driver    = carpoolDb.Drivers.FirstOrDefault(c => c.Id.Equals(ride.DriverId));
         Promotion promotion = carpoolDb.Promotions.FirstOrDefault(c => c.Id.Equals(driver.PromotionId));
         if (rider == null)
         {
             rider = new Rider(user.Id, user.Id);
             carpoolDb.Riders.Add(rider);
         }
         var amount         = Math.Round(model.Distance * Constants.Price, 2, MidpointRounding.ToEven);
         var driverDiscount = 0.0;
         if (model.Distance > promotion.Distance)
         {
             driverDiscount = promotion.Discount * amount;
         }
         var appDiscount = 0.0;
         if (model.Distance > Constants.MinimumDistance)
         {
             appDiscount = Constants.AppDiscount * amount;
         }
         var  cgst = (amount - driverDiscount - appDiscount) * Constants.CGST;
         var  sgst = (amount - driverDiscount - appDiscount) * Constants.SGST;
         Bill bill = new Bill(HelperService.GenerateId("BIL"), ride.DriverId, rider.Id, amount, sgst, cgst, driverDiscount, appDiscount);
         carpoolDb.Bills.Add(bill);
         RideRequest rideRequest = new RideRequest(
             HelperService.GenerateId("REQ"),
             model.RideId,
             rider.Id,
             ride.DriverId,
             model.Source.Id,
             model.Destination.Id,
             bill.Id
             );
         carpoolDb.RideRequests.Add(rideRequest);
         carpoolDb.SaveChanges();
         return("Ok");
     }
     catch (Exception)
     {
         return("Ride could not be offered");
     }
 }
        public ActionResult <Fahrgemeinschaft> Post(FahrgemeinschaftCreateRequest fcr)
        {
            var sub = HttpContext.User.Claims.FirstOrDefault(c =>
                                                             c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier")
                      ?.Value;

            var user = _users.SingleOrDefault(user =>
                                              user.OauthId == sub);

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

            var fahrgemeinschaft = new Fahrgemeinschaft
            {
                CreatorId = user.Id,
                Name      = fcr.Name,
                FahrgemeinschaftMitglieds = { new FahrgemeinschaftMitglied {
                                                  UserId = user.Id
                                              } }
            };

            _fahrgemeinschafts.Add(fahrgemeinschaft);
            _cpctx.SaveChanges();
            return(CreatedAtAction(nameof(Get), new { id = fahrgemeinschaft.Id }, fahrgemeinschaft));
        }
 protected void ClearDatabaseForConcurrencyReasons()
 {
     DbContext.RemoveRange(DbContext.Cars);
     DbContext.RemoveRange(DbContext.Locations);
     DbContext.RemoveRange(DbContext.Employees);
     DbContext.RemoveRange(DbContext.TravelPlanEmployees);
     DbContext.RemoveRange(DbContext.TravelPlans);
     DbContext.SaveChanges();
 }
예제 #5
0
 public string CreateRide(OfferRequest model, ApplicationUser user)
 {
     try
     {
         Driver driver = carpoolDb.Drivers.FirstOrDefault(c => c.ApplicationUserId.Equals(user.Id));
         if (driver == null)
         {
             return("Driver not registered");
         }
         var           rideId      = HelperService.GenerateId("RID");
         List <string> vaiPointIds = new List <string>();
         AddLocation(model.Source);
         AddLocation(model.Destination);
         vaiPointIds.Add(model.Source.Id);
         foreach (Location loc in model.ViaPoints)
         {
             AddLocation(loc);
             vaiPointIds.Add(loc.Id);
         }
         vaiPointIds.Add(model.Destination.Id);
         Ride ride = new Ride
                     (
             rideId,
             driver.Id,
             Convert.ToDateTime(model.Date),
             model.Time,
             model.Source.Id,
             model.Destination.Id,
             String.Join(",", vaiPointIds)
                     );
         carpoolDb.Rides.Add(ride);
         List <Seat> seats = new List <Seat>();
         for (int j = 0; j < Convert.ToInt32(model.Seats); j++)
         {
             Seat seat = new Seat(HelperService.GenerateId("SEA") + Convert.ToString(j), ride.Id);
             seats.Add(seat);
         }
         carpoolDb.Seats.AddRange(seats);
         carpoolDb.SaveChanges();
         return("Ok");
     }
     catch (Exception e)
     {
         return(e.Message);
     }
 }
예제 #6
0
        public User Update(ApplicationUser user, User model)
        {
            user.Name               = model.Name;
            user.PhoneNumber        = model.PhoneNumber;
            user.Email              = model.Email;
            user.UserName           = model.Email;
            user.NormalizedEmail    = model.Email.ToUpper();
            user.NormalizedUserName = model.Email.ToUpper();
            carpoolDb.ApplicationUsers.Update(user);
            carpoolDb.SaveChanges();
            User userResponse = new User()
            {
                Name        = user.Name,
                PhoneNumber = user.PhoneNumber,
                Email       = user.Email
            };

            return(userResponse);
        }
예제 #7
0
        public ActionResult <User> Register(User userIn)
        {
            var sub = HttpContext.User.Claims.FirstOrDefault(c =>
                                                             c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier")
                      ?.Value;
            var user = _users.SingleOrDefault(user => user.OauthId == sub);

            if (user == null)
            {
                userIn.OauthId = sub;
                _users.Add(userIn);
                _cpctx.SaveChanges();

                return(Ok(userIn));
            }
            else
            {
                return(Login());
            }
        }
예제 #8
0
        public Model.Korisnik Insert(KorisnikInsertRequest request)
        {
            var getAll = _context.Korisnici.ToList();

            foreach (var k in getAll)
            {
                if (k.KorisnickoIme == request.KorisnickoIme)
                {
                    throw new UserException("Korisničko ime već postoji!");
                }
                if (k.Email == request.Email)
                {
                    throw new UserException("Već postoji profil registrovan na " + request.Email);
                }
            }

            var entity = _mapper.Map <Database.Korisnik>(request);

            if (request.Password != request.PasswordConfirmation)
            {
                throw new UserException("Passwordi se ne slazu");
            }

            entity.LozinkaSalt = GenerateSalt();
            entity.LozinkaHash = GenerateHash(entity.LozinkaSalt, request.Password);
            entity.IsVozac     = false;

            _context.Korisnici.Add(entity);
            _context.SaveChanges();

            if (request.Uloge.Count != 0)
            {
                foreach (var uloga in request.Uloge)
                {
                    entity.KorisniciUloge.Add(new Database.KorisniciUloge
                    {
                        KorisnikId   = entity.KorisnikID,
                        DatumIzmjene = DateTime.Now,
                        UlogaId      = uloga
                    });
                }
            }

            _context.SaveChanges();

            var model = new Model.Korisnik
            {
                KorisnikID    = entity.KorisnikID,
                BrojTelefona  = entity.BrojTelefona,
                DatumRodjenja = entity.DatumRodjenja,
                Email         = entity.Email,
                GradID        = entity.GradID,
                Ime           = entity.Ime,
                IsVozac       = entity.IsVozac,
                KorisnickoIme = entity.KorisnickoIme,
                Prezime       = entity.Prezime,
                Slika         = entity.Slika
            };

            model.KorisniciUloge = new List <Model.KorisniciUloge>();
            foreach (var item in entity.KorisniciUloge)
            {
                model.KorisniciUloge.Add(new Model.KorisniciUloge
                {
                    DatumIzmjene    = item.DatumIzmjene,
                    KorisnikId      = item.KorisnikId,
                    KorisnikUlogaId = item.KorisnikUlogaId,
                    UlogaId         = item.UlogaId
                });
            }

            return(model);
        }
예제 #9
0
        public void AddTrip(NewPostingViewModel viewModel, User user)
        {
            using (Context) {
                Location to = new Location();
                to.StreetAddress = viewModel.StreetAddressDestination;
                to.City          = viewModel.CityDestination;
                to.State         = viewModel.StateDestination;
                to.ZipCode       = viewModel.ZipDestination;

                Location from = new Location();
                from.StreetAddress = viewModel.StreetAddressOrigin;
                from.City          = viewModel.CityOrigin;
                from.State         = viewModel.StateOrigin;
                from.ZipCode       = viewModel.ZipOrigin;

                Trip trip = new Trip();
                trip.To             = to;
                trip.From           = from;
                trip.TravelDateTime = viewModel.DateTime;
                trip.Type           = viewModel.TripType;

                TripInformation tripInformation = new TripInformation();
                tripInformation.Capacity = viewModel.TripType == TripType.LookingForPassengers ? 3 : 1;
                trip.TripInformation     = tripInformation;
                using (CarpoolContext context = new CarpoolContext()) {
                    user = context.Users.Single(x => x.UserId == user.UserId);
                    trip.TripInformation.Owner = user;
                    context.Trips.Add(trip);
                    AddressBook addressBook      = context.AddressBooks.SingleOrDefault(x => x.UserId == user.UserId);
                    bool        isNewAddressBook = addressBook == null;
                    if (viewModel.AddOriginToAddressBook)
                    {
                        if (addressBook == null)
                        {
                            addressBook = new AddressBook()
                            {
                                User = user, UserId = user.UserId, Locations = new List <Location>()
                            };
                        }
                        addressBook.Locations.Add(from);
                    }
                    if (viewModel.AddDestinationToAddressBook)
                    {
                        if (addressBook == null)
                        {
                            addressBook = new AddressBook()
                            {
                                User = user, UserId = user.UserId, Locations = new List <Location>()
                            };
                        }
                        addressBook.Locations.Add(to);
                    }
                    if (viewModel.AddOriginToAddressBook || viewModel.AddDestinationToAddressBook)
                    {
                        if (isNewAddressBook)
                        {
                            context.AddressBooks.Add(addressBook);
                        }
                        else
                        {
                            context.Set <AddressBook>().Attach(addressBook);
                            context.Entry(addressBook).State = EntityState.Modified;
                        }
                    }
                    context.SaveChanges();
                }
            }
        }