public async Task <IActionResult> PutProduct(int id, Product product)
        {
            if (id != product.Id)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
Exemple #2
0
        public async Task <IActionResult> PostInvoice(Invoice invoice)
        {
            try
            {
                // Getting and checking that CabinOwner owns the Cabin what is in Invoice
                if (User.IsInRole("CabinOwner"))
                {
                    var cabinReservation = await _context.CabinReservation.Where(cabinReservation => cabinReservation.CabinReservationId == invoice.CabinReservationId)
                                           .Include(cabinReservation => cabinReservation.Cabin).ThenInclude(cabin => cabin.Person).FirstOrDefaultAsync();

                    if (cabinReservation.Cabin.Person.Email != User.Identity.Name)
                    {
                        return(Unauthorized());
                    }
                }

                _context.Invoice.Add(invoice);
                await _context.SaveChangesAsync();

                return(NoContent());
            }
            catch
            {
                return(StatusCode(500));
            }
        }
Exemple #3
0
        public async Task <IActionResult> PutSession(int id, Session session)
        {
            if (id != session.ID)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
Exemple #4
0
        public async Task <IActionResult> PutPerson(int id, Person person)
        {
            try
            {
                // Checks that User-role is Administrator or User in IdentityDB matches Person in CabinReservationsDB
                if (false == User.IsInRole("Administrator") && person.Email != User.Identity.Name)
                {
                    return(Unauthorized());
                }

                if (id != person.PersonId)
                {
                    return(BadRequest());
                }

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

                await _context.SaveChangesAsync();

                return(NoContent());
            }

            catch (DbUpdateConcurrencyException)
            {
                if (!PersonExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
        }
Exemple #5
0
        public async Task <IActionResult> PutActivity(int id, Activity activity)
        {
            if (id != activity.ActivityId)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
        public async Task <ActionResult <CabinImage> > PostCabinImage(CabinImage cabinImage)
        {
            try
            {
                // If CabinOwner, check that he can create Cabin only by own PersonId
                if (User.IsInRole("CabinOwner"))
                {
                    var checkPerson = await _context.Person.Where(person => person.Email == User.Identity.Name).FirstOrDefaultAsync();

                    var cabin = await _context.Cabin.Where(cabin => cabin.CabinId == cabinImage.CabinId).FirstOrDefaultAsync();

                    if (cabin.PersonId != checkPerson.PersonId)
                    {
                        return(Unauthorized());
                    }
                }

                // Return BadRequest if cabin has already 4 images
                var cabinImages = await _context.CabinImage.Where(cImage => cImage.CabinId == cabinImage.CabinId).ToListAsync();

                if (cabinImages.Count() >= 4)
                {
                    return(BadRequest());
                }

                _context.CabinImage.Add(cabinImage);
                await _context.SaveChangesAsync();

                return(NoContent());
            }
            catch
            {
                return(StatusCode(500));
            }
        }
Exemple #7
0
        public async Task <ActionResult <Resort> > PostResort([FromBody] Resort resort)
        {
            try
            {
                _context.Resort.Add(resort);
                await _context.SaveChangesAsync();

                return(NoContent());
            }
            catch
            {
                return(StatusCode(400));
            }
        }
Exemple #8
0
        public async Task <IActionResult> PutProduct(int id, Product product)
        {
            if (id != product.Id)
            {
                return(BadRequest());
            }
            Product original = await _context.Product.AsNoTracking <Product>().FirstOrDefaultAsync(p => p.Id == id);

            AuthorizationResult authresult = await _authorizationService.AuthorizeAsync(User, original, "ProductOwner");

            if (!authresult.Succeeded)
            {
                if (User.Identity.IsAuthenticated)
                {
                    return(new ForbidResult());
                }
                else
                {
                    return(new ChallengeResult());
                }
            }

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

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

            return(NoContent());
        }
        public async Task <ActionResult <CabinReservation> > PostCabinReservation(CabinReservation cabinReservation)
        {
            try
            {
                // Last moment "kikkailua", had to do this because locally it works great, but when publishing it sets Start- and EndDates 1 day greater than selected
                // This may not work all cases/timezones
                //cabinReservation.ReservationStartDate = cabinReservation.ReservationStartDate.ToUniversalTime();
                //cabinReservation.ReservationEndDate = cabinReservation.ReservationEndDate.ToUniversalTime();
                cabinReservation.ReservationStartDate = cabinReservation.ReservationStartDate.AddHours(3);
                cabinReservation.ReservationEndDate   = cabinReservation.ReservationEndDate.AddHours(3);
                cabinReservation.ReservationStartDate = cabinReservation.ReservationStartDate.Date;
                cabinReservation.ReservationEndDate   = cabinReservation.ReservationEndDate.Date;

                if (cabinReservation.ReservationStartDate >= cabinReservation.ReservationEndDate)
                {
                    return(BadRequest());
                }

                // Getting list of days between ReservationStartDate - ReservationEndDate
                var suggestedDays = Enumerable.Range(0, 1 + cabinReservation.ReservationEndDate.Subtract(cabinReservation.ReservationStartDate).Days)
                                    .Select(offset => cabinReservation.ReservationStartDate.AddDays(offset))
                                    .ToArray();
                // Checking that Cabin is not reserved by given time
                foreach (var item in suggestedDays)
                {
                    var checkAvailability = await _context.CabinReservation
                                            .Where(cabinRes => cabinRes.CabinId == cabinReservation.CabinId)
                                            .Where(cabinRes => cabinRes.ReservationStartDate.AddDays(1) == item || cabinRes.ReservationEndDate.AddDays(-1) == item)
                                            .FirstOrDefaultAsync();

                    if (checkAvailability != null)
                    {
                        return(BadRequest());
                    }
                }


                _context.Add(cabinReservation);

                if (User.IsInRole("CabinOwner") || User.IsInRole("Customer"))
                {
                    cabinReservation.Person = await _context.Person.Where(person => person.Email == User.Identity.Name).FirstOrDefaultAsync();

                    cabinReservation.PersonId = cabinReservation.Person.PersonId;
                }
                else if (User.IsInRole("Administrator"))
                {
                    cabinReservation.Person = await _context.Person.Where(person => person.PersonId == cabinReservation.PersonId).FirstOrDefaultAsync();
                }

                else
                {
                    return(Unauthorized());
                }

                cabinReservation.ReservationBookingTime = DateTime.Now;

                // Counting Activities TotalPrice
                decimal activitiesTotalPrice = 0;
                if (cabinReservation.ActivityReservations != null)
                {
                    foreach (var item in cabinReservation.ActivityReservations)
                    {
                        var activity = await _context.Activity.Where(activity => activity.ActivityId == item.ActivityId).FirstOrDefaultAsync();

                        activitiesTotalPrice += activity.ActivityPrice;
                    }
                }

                // Creating and adding new Invoice to CabinReservation
                Invoice invoice = new Invoice();
                var     cabin   = await _context.Cabin.Where(cabin => cabin.CabinId == cabinReservation.CabinId).FirstOrDefaultAsync();

                decimal duration = (cabinReservation.ReservationEndDate - cabinReservation.ReservationStartDate).Days;
                invoice.DateOfExpiry       = cabinReservation.ReservationEndDate.AddDays(30);
                invoice.InvoiceTotalAmount = (duration * cabin.CabinPricePerDay) + activitiesTotalPrice;

                cabinReservation.Invoices = new List <Invoice>();
                cabinReservation.Invoices.Add(invoice);

                await _context.SaveChangesAsync();

                return(NoContent());
            }
            catch
            {
                return(StatusCode(500));
            }
        }