public async Task <IActionResult> POST([FromBody] NapSpace newNapSpace)
        {
            ModelState.Remove("User");
            User user = await _context.User.Where(u => u.UserName == User.Identity.Name).SingleOrDefaultAsync();

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

            newNapSpace.User = user;

            _context.NapSpace.Add(newNapSpace);

            try
            {
                _context.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (NapSpaceExists(newNapSpace.NapSpaceId))
                {
                    return(new StatusCodeResult(StatusCodes.Status409Conflict));
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtRoute("GetSingleNapSpace", new { id = newNapSpace.NapSpaceId }, newNapSpace));
        }
        public async Task <IActionResult> PUT(int id, [FromBody] NapSpace newNapSpace)
        {
            ModelState.Remove("User");
            User user = await _context.User.Where(u => u.UserName == User.Identity.Name).SingleOrDefaultAsync();

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (id != newNapSpace.NapSpaceId)
            {
                return(BadRequest());
            }
            newNapSpace.User = user;

            _context.NapSpace.Update(newNapSpace);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!NapSpaceExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(new StatusCodeResult(StatusCodes.Status200OK));
        }
        public async Task <IActionResult> DELETE(int id)
        {
            ModelState.Remove("User");
            User user = await _context.User.Where(u => u.UserName == User.Identity.Name).SingleOrDefaultAsync();

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

            NapSpace napSpace = _context.NapSpace.Single(m => m.NapSpaceId == id);

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

            _context.NapSpace.Remove(napSpace);
            _context.SaveChanges();

            return(Ok(napSpace));
        }
        public IActionResult GET(int id)
        {
            var NapSpace = _context.NapSpace.Include("User").ToList();

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            try
            {
                NapSpace napSpace = _context.NapSpace.Single(n => n.NapSpaceId == id);

                if (napSpace == null)
                {
                    return(NotFound());
                }
                return(Ok(napSpace));
            }
            catch (System.InvalidOperationException ex)
            {
                return(NotFound(ex));
            }
        }
Beispiel #5
0
        public async static void Initialize(IServiceProvider serviceProvider)
        {
            var context = serviceProvider.GetRequiredService <BYOBDbContext>();

            context.Database.EnsureCreated();

            var roleStore = new RoleStore <IdentityRole>(context);
            var userstore = new UserStore <User>(context);

            if (!context.User.Any(u => u.FirstName == "Jesse"))
            {
                //  This method will be called after migrating to the latest version.
                User user = new User
                {
                    FirstName          = "Jesse",
                    LastName           = "Page",
                    UserName           = "******",
                    NormalizedUserName = "******",
                    Email           = "*****@*****.**",
                    NormalizedEmail = "*****@*****.**",
                    EmailConfirmed  = true,
                    LockoutEnabled  = false,
                    SecurityStamp   = Guid.NewGuid().ToString("D")
                };
                var passwordHash = new PasswordHasher <User>();
                user.PasswordHash = passwordHash.HashPassword(user, "jjjjjj");
                await userstore.CreateAsync(user);
            }

            // Look for any products.
            if (context.NapSpace.Any())
            {
                return;       // DB has been seeded
            }

            var napSpace = new NapSpace[]
            {
                new NapSpace {
                    Title       = "Big Comfy Couch",
                    Description = "Oh man this couch will eat you",
                    Price       = "$5",
                    Rules       = "Please don't drool on the pillows",
                    Payment     = "PayPal",
                    Address     = "123 Buttz Lane",
                    PictureURL  = "pic.com",
                    User        = context.User.Single(n => n.Id == "8af4fde9-153f-47b8-bdc2-12ce342de91b")
                },
                new NapSpace {
                    Title       = "Tent",
                    Description = "Sick ass tent",
                    Price       = "$3.50",
                    Rules       = "It's for glamping only!",
                    Payment     = "Cash Money",
                    Address     = "1111 Yep Rd",
                    PictureURL  = "pic.com",
                    User        = context.User.Single(n => n.Id == "8af4fde9-153f-47b8-bdc2-12ce342de91b")
                }
            };

            foreach (NapSpace n in napSpace)
            {
                context.NapSpace.Add(n);
            }
            context.SaveChanges();

            var reservation = new Reservation[]
            {
                new Reservation {
                    NapSpaceTitle = "Tent",
                    CalendarColor = "#5f6dd0",
                    StartDateTime = new DateTime(2018, 3, 15, 12, 0, 0),
                    EndDateTime   = new DateTime(2018, 3, 15, 1, 0, 0),
                    NapSpaceId    = context.NapSpace.Single(n => n.Title == "Tent").NapSpaceId,
                    User          = context.User.Single(n => n.Email == "*****@*****.**")
                },
                new Reservation
                {
                    NapSpaceTitle = "Big Comfy Couch",
                    CalendarColor = "#5f6dd0",
                    StartDateTime = new DateTime(2018, 3, 15, 11, 0, 0),
                    EndDateTime   = new DateTime(2018, 3, 15, 12, 0, 0),
                    NapSpaceId    = context.NapSpace.Single(n => n.Title == "Big Comfy Couch").NapSpaceId,
                    User          = context.User.Single(n => n.Email == "*****@*****.**")
                }
            };

            foreach (Reservation r in reservation)
            {
                context.Reservation.Add(r);
            }
            context.SaveChanges();
        }