Exemplo n.º 1
0
        public async Task <IActionResult> Post([FromBody] BoatCalendarViewModel boatCalendarViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            // find a boat by given calendarboatid
            var boat = await _context.Boats.SingleOrDefaultAsync(o => o.Id == boatCalendarViewModel.CalendarBoatId);

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

            // find row in table (if exists) for that date (table BoatCalendars) -> this boat id
            // if exists update
            // if not insert

            var boatCalendar = await _context.BoatCalendars.FirstOrDefaultAsync(
                o => o.CalendarBoatId == boatCalendarViewModel.CalendarBoatId &&
                o.BookDate == boatCalendarViewModel.BookDate);

            if (boatCalendar == null)
            {
                boatCalendar = new BoatCalendar
                {
                    BookDate          = boatCalendarViewModel.BookDate,
                    FullDayPrice      = boatCalendarViewModel.FullDayPrice,
                    MorningPrice      = boatCalendarViewModel.MorningPrice,
                    AfternoonPrice    = boatCalendarViewModel.AfternoonPrice,
                    FourteenDaysPrice = boatCalendarViewModel.FourteenDaysPrice,
                    SevenDaysPrice    = boatCalendarViewModel.SevenDaysPrice,
                    CalendarBoatId    = boatCalendarViewModel.CalendarBoatId,
                    Boat = boat
                };

                _context.BoatCalendars.Add(boatCalendar);
                await _context.SaveChangesAsync();
            }
            else
            {
                // only this fields are allowed for update

                var oldFullDayPrice      = boatCalendar.FullDayPrice;
                var oldMorningPrice      = boatCalendar.MorningPrice;
                var oldAfternoonPrice    = boatCalendar.AfternoonPrice;
                var oldFourteenDaysPrice = boatCalendar.FourteenDaysPrice;
                var oldSevenDaysPrice    = boatCalendar.SevenDaysPrice;

                var oldCalendarBoatId = boatCalendar.CalendarBoatId;
                var oldBookDate       = boatCalendar.BookDate;

                // remove old row -> not needed we insert new one

                _context.Remove(boatCalendar);
                _context.SaveChanges();

                boatCalendar = new BoatCalendar {
                };

                boatCalendar.BookDate = oldBookDate;

                boatCalendar.FullDayPrice      = ChangeOrNot(oldFullDayPrice, boatCalendarViewModel.FullDayPrice) ? boatCalendarViewModel.FullDayPrice : oldFullDayPrice;
                boatCalendar.MorningPrice      = ChangeOrNot(oldMorningPrice, boatCalendarViewModel.MorningPrice) ? boatCalendarViewModel.MorningPrice : oldMorningPrice;
                boatCalendar.AfternoonPrice    = ChangeOrNot(oldAfternoonPrice, boatCalendarViewModel.AfternoonPrice) ? boatCalendarViewModel.AfternoonPrice : oldAfternoonPrice;
                boatCalendar.FourteenDaysPrice = ChangeOrNot(oldFourteenDaysPrice, boatCalendarViewModel.FourteenDaysPrice) ? boatCalendarViewModel.FourteenDaysPrice : oldFourteenDaysPrice;
                boatCalendar.SevenDaysPrice    = ChangeOrNot(oldSevenDaysPrice, boatCalendarViewModel.SevenDaysPrice) ? boatCalendarViewModel.SevenDaysPrice : oldSevenDaysPrice;


                boatCalendar.CalendarBoatId = oldCalendarBoatId;
                boatCalendar.Boat           = boat;



                _context.BoatCalendars.Add(boatCalendar);
                await _context.SaveChangesAsync();
            };

            return(Ok(boatCalendar));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> PostDateRange([FromBody] BoatCalendarDateRangeViewModel boatCalendarDateRangeViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            // find a boat by given calendarboatid
            var boat = await _context.Boats.SingleOrDefaultAsync(o => o.Id == boatCalendarDateRangeViewModel.CalendarBoatId);

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

            // find row in table (if exists) for that date (table BoatCalendars) -> this boat id
            // if exists update
            // if not insert

            var     dateX    = boatCalendarDateRangeViewModel.StartDate;
            Boolean dateloop = true;
            int     maxloop  = 365; // set here how many days can be in range max
            int     brojac   = 0;

            while (dateloop)
            {
                var boatCalendar = _context.BoatCalendars.FirstOrDefault(
                    o => o.CalendarBoatId == boatCalendarDateRangeViewModel.CalendarBoatId &&
                    o.BookDate == dateX);



                if (boatCalendar == null)
                {
                    boatCalendar = new BoatCalendar
                    {
                        BookDate          = dateX,
                        FullDayPrice      = boatCalendarDateRangeViewModel.FullDayPrice,
                        MorningPrice      = boatCalendarDateRangeViewModel.MorningPrice,
                        AfternoonPrice    = boatCalendarDateRangeViewModel.AfternoonPrice,
                        FourteenDaysPrice = boatCalendarDateRangeViewModel.FourteenDaysPrice,
                        SevenDaysPrice    = boatCalendarDateRangeViewModel.SevenDaysPrice,
                        CalendarBoatId    = boatCalendarDateRangeViewModel.CalendarBoatId,
                        Boat = boat
                    };

                    _context.BoatCalendars.Add(boatCalendar);
                    await _context.SaveChangesAsync();
                }
                else
                {
                    // only this fields are allowed for update

                    var oldFullDayPrice      = boatCalendar.FullDayPrice;
                    var oldMorningPrice      = boatCalendar.MorningPrice;
                    var oldAfternoonPrice    = boatCalendar.AfternoonPrice;
                    var oldFourteenDaysPrice = boatCalendar.FourteenDaysPrice;
                    var oldSevenDaysPrice    = boatCalendar.SevenDaysPrice;

                    var oldCalendarBoatId = boatCalendar.CalendarBoatId;
                    var oldBookDate       = boatCalendar.BookDate;

                    // remove old row -> not needed we insert new one

                    _context.Remove(boatCalendar);
                    _context.SaveChanges();

                    boatCalendar = new BoatCalendar {
                    };

                    boatCalendar.BookDate = oldBookDate;

                    boatCalendar.FullDayPrice      = ChangeOrNot(oldFullDayPrice, boatCalendarDateRangeViewModel.FullDayPrice) ? boatCalendarDateRangeViewModel.FullDayPrice : oldFullDayPrice;
                    boatCalendar.MorningPrice      = ChangeOrNot(oldMorningPrice, boatCalendarDateRangeViewModel.MorningPrice) ? boatCalendarDateRangeViewModel.MorningPrice : oldMorningPrice;
                    boatCalendar.AfternoonPrice    = ChangeOrNot(oldAfternoonPrice, boatCalendarDateRangeViewModel.AfternoonPrice) ? boatCalendarDateRangeViewModel.AfternoonPrice : oldAfternoonPrice;
                    boatCalendar.FourteenDaysPrice = ChangeOrNot(oldFourteenDaysPrice, boatCalendarDateRangeViewModel.FourteenDaysPrice) ? boatCalendarDateRangeViewModel.FourteenDaysPrice : oldFourteenDaysPrice;
                    boatCalendar.SevenDaysPrice    = ChangeOrNot(oldSevenDaysPrice, boatCalendarDateRangeViewModel.SevenDaysPrice) ? boatCalendarDateRangeViewModel.SevenDaysPrice : oldSevenDaysPrice;


                    boatCalendar.CalendarBoatId = oldCalendarBoatId;
                    boatCalendar.Boat           = boat;



                    _context.BoatCalendars.Add(boatCalendar);
                    await _context.SaveChangesAsync();
                }

                dateX = dateX.AddDays(1);

                brojac++;

                if (dateX > boatCalendarDateRangeViewModel.EndDate)
                {
                    dateloop = false;
                }
                // nesmijemo dozvoliti neograniceni date range odjednom zbog broja requestova ( memory )
                if (brojac >= maxloop)
                {
                    dateloop = false;
                }
            } // while

            // date is changed in each iterration

            return(Ok());
        }