Beispiel #1
0
        public async Task <IActionResult> PutRequest(int id, Request request)
        {
            if (id != request.Id)
            {
                return(BadRequest());
            }

            _context.Entry(request).State = EntityState.Modified;

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

            return(NoContent());
        }
Beispiel #2
0
        public async Task <IActionResult> AddToCart(int copyId)
        {
            var copy = await _context.BookCopies.FirstAsync(x => x.CopyID == copyId);

            if (!copy.IsAvailable)
            {
                return(BadRequest());
            }

            var waitlist = await _context.Waitlists.FirstAsync(x => x.ISBN == copy.ISBN);

            var peopleWaiting = _context.WaitingPeople.Where(x => x.WaitlistListID == waitlist.ListID).ToList();

            if (peopleWaiting.Count > 0)
            {
                return(BadRequest());
            }

            copy.IsAvailable = false;
            _context.BookCopies.Update(copy);
            var userEmail = HttpContext.Items["Email"] as string;
            var user      = await _context.Users.FirstAsync(x => x.Email == userEmail);

            var userId = user.UserId ?? default(int);
            await _context.Carts.AddAsync(new Cart(userId, copy.CopyID ?? default(int)));

            await _context.SaveChangesAsync();

            return(Ok());
        }
Beispiel #3
0
        public async Task <IActionResult> AddToCart(string isbn)
        {
            var copy = await _context.BookCopies.FirstAsync(x => x.ISBN == isbn && x.IsAvailable == true);

            var userEmail = HttpContext.Items["Email"] as string;
            var user      = await _context.Users.FirstAsync(x => x.Email == userEmail);

            var userId = user.UserId ?? default(int);

            if (!copy.IsAvailable)
            {
                return(BadRequest());
            }

            var waitlist = await _context.Waitlists.FirstAsync(x => x.ISBN == copy.ISBN);

            var meWaiting =
                await _context.WaitingPeople.FirstAsync(x => x.WaitlistListID == waitlist.ListID && x.UserUserID == userId);

            _context.WaitingPeople.Remove(meWaiting);
            copy.IsAvailable = false;
            _context.BookCopies.Update(copy);

            await _context.Carts.AddAsync(new Cart(userId, copy.CopyID ?? default(int)));

            await _context.SaveChangesAsync();

            return(Ok());
        }
Beispiel #4
0
        public async Task Invoke(HttpContext httpContext, DbAppContext db)
        {
            await _next.Invoke(httpContext);

            if (db.ChangeTracker.HasChanges())
            {
                await db.SaveChangesAsync();
            }
        }
Beispiel #5
0
        // Add new User
        public async Task AddUserAsync(string username)
        {
            User user = new User();

            user.UserName = username;
            await _context.AddAsync(user);

            await _context.SaveChangesAsync();
        }
Beispiel #6
0
        public async Task <string> RegisterUser(RegisterRequest req)
        {
            var otherUsers = await _context.Users.FirstOrDefaultAsync(x => x.Email == req.Email);

            if (otherUsers != null)
            {
                return(null);
            }

            var newSalt    = GetSalt();
            var hashedPass = HashPassword(req.Password, newSalt);

            var user = new User(req.Name, req.Email, hashedPass, newSalt);
            await _context.Users.AddAsync(user);

            await _context.SaveChangesAsync();

            return(generateJwtToken(user.Email));
        }
Beispiel #7
0
        // Add room - not use atm
        public async Task AddRoomAsync(Room room)
        {
            await _context.Rooms.AddAsync(room);

            await _context.SaveChangesAsync();
        }
        // Add new reservations from Form
        public async Task AddReservationAsync(Reservation reservation)
        {
            await _context.Reservations.AddAsync(reservation);

            await _context.SaveChangesAsync();
        }