Beispiel #1
0
        public IHttpActionResult PostTravel(ShareDTO infos)
        {

            string username = infos.username;
            int travelId = infos.travelid;

            Travel trav = db.Travels.Find(travelId);

            UserStore<ApplicationUser> userStore = new UserStore<ApplicationUser>(db);
            UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>(userStore);
            ApplicationUser user = userManager.FindByEmail(username);

            
            if(!trav.ApplicationUsers.Contains(user))
            {
                if(user.Travels == null)
                {
                    user.Travels = new List<Travel>();
                }
                user.Travels.Add(trav);
            }

            db.SaveChanges();

            TravelDTO dto = new TravelDTO(trav);

            return Ok(dto);
        }
        public IHttpActionResult PostTravel(Travel travel)
        {

            UserStore<ApplicationUser> userStore = new UserStore<ApplicationUser>(db);
            UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>(userStore);
            ApplicationUser user = userManager.FindById(User.Identity.GetUserId());

            if (travel.ApplicationUsers == null)
            {
                travel.ApplicationUsers = new List<ApplicationUser>();
            }

            travel.ApplicationUsers.Add(user);

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Travels.Add(travel);
            db.SaveChanges();

            if(user.Travels == null)
            {
                user.Travels = new List<Travel>();
            }

            user.Travels.Add(travel);
            db.SaveChanges();

            TravelDTO travelDTO = new TravelDTO(travel);

            return Ok(travelDTO);
        }