コード例 #1
0
        public ActionResult AddInternShip([FromBody] InternShipRequest req)
        {
            if (req != null)
            {
                var internShip = new InternShip()
                {
                    finishDate = req.finishDate,
                    startDate  = req.startDate,
                    studentId  = req.studentId
                };
                _appRepository.Add(internShip);
                _appRepository.SaveAll();
                _appRepository.createDays(req);
                return(StatusCode(201));
            }

            return(null);
        }
コード例 #2
0
        public DateTime[] createDays(InternShipRequest req)
        {
            DateTime firstDay = req.startDate.Date;
            DateTime lastDay  = req.finishDate.Date;

            //DateTime[] bankHolidays;
            if (firstDay > lastDay)
            {
                throw new ArgumentException("Incorrect last day " + lastDay);
            }

            TimeSpan span          = lastDay - firstDay;
            int      businessDays  = span.Days + 1;
            int      fullWeekCount = businessDays / 7;

            // find out if there are weekends during the time exceedng the full weeks
            if (businessDays > fullWeekCount * 7)
            {
                // we are here to find out if there is a 1-day or 2-days weekend
                // in the time interval remaining after subtracting the complete weeks
                int firstDayOfWeek = (int)firstDay.DayOfWeek;
                int lastDayOfWeek  = (int)lastDay.DayOfWeek;
                if (lastDayOfWeek < firstDayOfWeek)
                {
                    lastDayOfWeek += 7;
                }
                if (firstDayOfWeek <= 6)
                {
                    if (lastDayOfWeek >= 7)// Both Saturday and Sunday are in the remaining time interval
                    {
                        businessDays -= 2;
                    }
                    else if (lastDayOfWeek >= 6)// Only Saturday is in the remaining time interval
                    {
                        businessDays -= 1;
                    }
                }
                else if (firstDayOfWeek <= 7 && lastDayOfWeek >= 7)// Only Sunday is in the remaining time interval
                {
                    businessDays -= 1;
                }
            }

            // subtract the weekends during the full weeks in the interval
            businessDays -= fullWeekCount + fullWeekCount;

            // subtract the number of bank holidays during the time interval
            //foreach (DateTime bankHoliday in bankHolidays)
            //{
            //    DateTime bh = bankHoliday.Date;
            //    if (firstDay <= bh && bh <= lastDay)
            //        --businessDays;
            //}

            DateTime[] dateTimes = new DateTime[businessDays];
            DateTime   now       = firstDay;

            for (int i = 0; i <= (lastDay - firstDay).TotalDays; i++)
            {
                if ((now.DayOfWeek != DayOfWeek.Saturday) && (now.DayOfWeek != DayOfWeek.Sunday))
                {
                    //dateTimes[i] = now;

                    var day = new Day()
                    {
                        studentId = req.studentId,
                        date      = now
                    };
                    _context.Add(day);
                    _context.SaveChanges();
                }

                now = now.AddDays(1);
            }
            return(dateTimes);
        }