Exemple #1
0
        public async Task <Tourist> SaveTouristModel(NewTouristForm model)
        {
            foreach (var t in model.Tours)
            {
                if (model.SelectedTourIds.Contains(t.TourId))
                {
                    t.selected = true;
                }
            }
            Tourist tourist = new Tourist();

            tourist.Fullname = model.Fullname;
            tourist.Hometown = model.Hometown;
            tourist.GuideId  = model.GuideId;
            tourist.Age      = model.Age;
            tourist.Avatar   = model.Path;
            List <TouristTour> touristTours = new List <TouristTour>();
            await db.Tourists.AddAsync(tourist);

            // сохраняем в бд все изменения
            await db.SaveChangesAsync();

            foreach (var t in model.Tours.Where(x => x.selected == true))
            {
                TouristTour touristTour = new TouristTour();
                touristTour.TouristId = tourist.Touristid;
                touristTour.TourId    = t.TourId;
                touristTours.Add(touristTour);
            }
            await db.TouristTour.AddRangeAsync(touristTours);

            await db.SaveChangesAsync();

            return(tourist);
        }
Exemple #2
0
        public async Task SaveEditedTourist(TouristEditForm model)
        {
            Tourist tourist = new Tourist();

            tourist.Fullname  = model.Fullname;
            tourist.Hometown  = model.Hometown;
            tourist.GuideId   = model.GuideId;
            tourist.Age       = model.Age;
            tourist.Touristid = model.Touristid;


            if (model.Avatar != null)
            {
                _fileManagerServ.DeletePhoto(model.Path);
                model.Path = await model.Avatar.PathReturn(_appEnvironment);
            }
            tourist.Avatar = model.Path;

            List <TouristTour> touristToursToRemove = new List <TouristTour>();

            touristToursToRemove = await db.TouristTour.Where(x => x.TouristId == tourist.Touristid).ToListAsync();

            db.RemoveRange(touristToursToRemove);
            await db.SaveChangesAsync();

            List <TouristTour> touristToursToAdd = new List <TouristTour>();

            foreach (var t in model.Tours.Where(x => x.selected == true))
            {
                TouristTour touristTour = new TouristTour();
                touristTour.TouristId = tourist.Touristid;
                touristTour.TourId    = t.TourId;
                touristToursToAdd.Add(touristTour);
            }
            db.TouristTour.AddRange(touristToursToAdd);
            await db.SaveChangesAsync();

            if (model.Touristid != 0)
            {
                db.Entry(tourist).State = EntityState.Modified;
                await db.SaveChangesAsync();
            }
        }