예제 #1
0
        public async Task AddPitch(PitchCreateViewModel pitch)
        {
            using (var transaction = await _context.Database.BeginTransactionAsync())
            {
                try
                {
                    Pitch newPitch = new Pitch()
                    {
                        Id            = Guid.NewGuid(),
                        LocalisationX = pitch.LocalisationX,
                        LocalisationY = pitch.LocalisationY,
                        Name          = pitch.Name,
                        SpotNumber    = pitch.SpotNumber
                    };
                    _context.Pitch.Add(newPitch);

                    string[] weekDays   = pitch.WeekDays.Split(";");
                    string[] startHours = pitch.StartHours.Split(";");
                    string[] endHours   = pitch.EndHours.Split(";");

                    var allOpenHours = _context.PitchOpenHours;

                    for (int i = 0; i < weekDays.Length - 1; i++)
                    {
                        var record = allOpenHours.Where(x => x.Id == newPitch.Id && x.WeekDay == int.Parse(weekDays[i]) && x.EndHour == endHours[i] && x.StartHour == startHours[i]).FirstOrDefault();

                        if (record == null)
                        {
                            PitchOpenHours openHours = new PitchOpenHours()
                            {
                                Id        = Guid.NewGuid(),
                                PitchId   = newPitch.Id,
                                WeekDay   = int.Parse(weekDays[i]),
                                EndHour   = endHours[i],
                                StartHour = startHours[i]
                            };
                            _context.PitchOpenHours.Add(openHours);
                        }
                        else
                        {
                            record.IsArchived = false;
                        }
                    }

                    _context.SaveChanges();

                    transaction.Commit();
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
        }
예제 #2
0
        public bool AddPitchAvability(DateTime startDate, DateTime endDate)
        {
            if (endDate < startDate || endDate == DateTime.MinValue || startDate == DateTime.MinValue)
            {
                return(false);
            }

            DateTime currentDate;

            try
            {
                currentDate = _context.PitchAvailability.Max(x => x.OpenDate);
            }
            catch (Exception e)
            {
                currentDate = startDate;
            }

            if (currentDate < startDate)
            {
                currentDate = startDate;
            }

            var openHours = _context.PitchOpenHours;

            while (currentDate <= endDate)
            {
                int day = (int)currentDate.DayOfWeek;
                if (day == 0) //because Sunday is 0 at enum and at DB is 7
                {
                    day = 7;
                }

                var currentOpenHours = openHours.Where(x => x.WeekDay == day);

                foreach (PitchOpenHours hours in currentOpenHours)
                {
                    _context.PitchAvailability.Add(new PitchAvailability()
                    {
                        Id               = Guid.NewGuid(),
                        OpenDate         = currentDate,
                        PitchId          = hours.PitchId,
                        PitchOpenHoursId = hours.Id
                    });
                }

                currentDate = currentDate.AddDays(1);
            }

            _context.SaveChanges();

            return(true);
        }