public ActionResult UpdateFieldSchedule(FieldScheduleCreateViewModel model)
        {
            var service = this.Service <IFieldScheduleService>();

            ResponseModel <FieldScheduleViewModel> response = null;

            DateTime startTime = new DateTime();

            DateTime endTime = new DateTime();

            try
            {
                startTime = DateTime.ParseExact(model.StartTime, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);

                endTime = DateTime.ParseExact(model.EndTime, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
            }
            catch (Exception)
            {
                response = ResponseModel <FieldScheduleViewModel> .CreateErrorResponse("Tạo lịch thất bại", "Lỗi định dạng thời gian");

                return(Json(response));
            }

            try {
                bool timeValid = true;

                FieldSchedule schedule = service.Get(model.Id);

                //if (!(schedule.StartTime == startTime && schedule.EndTime == endTime && schedule.FieldId == model.FieldId)) {
                //    timeValid = service.checkTimeValidInFieldSchedule(model.FieldId, startTime.TimeOfDay, endTime.TimeOfDay, startTime, endTime);
                //}

                if (timeValid)
                {
                    schedule.FieldId = model.FieldId;
                    //schedule.StartTime = startTime;
                    //schedule.EndTime = endTime;
                    schedule.Type        = model.Type;
                    schedule.Description = model.Description;
                    service.Update(schedule);
                    service.Save();

                    FieldScheduleViewModel result = PrepareFieldScheduleViewModel(schedule);

                    response = new ResponseModel <FieldScheduleViewModel>(true, "Cập nhật lịch sân thành công", null, result);
                }
                else
                {
                    response = ResponseModel <FieldScheduleViewModel> .CreateErrorResponse("Cập nhật lịch thất bại", "Khung giờ đã tồn tại");
                }
            } catch (Exception) {
                response = ResponseModel <FieldScheduleViewModel> .CreateErrorResponse("Cập nhật lịch thất bại", systemError);
            }

            return(Json(response));
        }
        public ActionResult CreateFieldSchedule(FieldScheduleCreateViewModel model)
        {
            var scheduleService = this.Service <IFieldScheduleService>();

            ResponseModel <FieldScheduleViewModel> response = null;

            DateTime startTime = new DateTime();

            DateTime endTime = new DateTime();

            try
            {
                startTime = DateTime.ParseExact(model.StartTime, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);

                endTime = DateTime.ParseExact(model.EndTime, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
            }
            catch (Exception)
            {
                response = ResponseModel <FieldScheduleViewModel> .CreateErrorResponse("Tạo lịch thất bại", "Lỗi định dạng thời gian");

                return(Json(response));
            }
            try
            {
                if (scheduleService.checkTimeValidInFieldSchedule(null, model.FieldId, startTime.TimeOfDay, endTime.TimeOfDay, startTime, endTime))
                {
                    FieldSchedule schedule = new FieldSchedule();
                    schedule.FieldId = model.FieldId;
                    schedule.Type    = model.Type;
                    //schedule.StartTime = startTime;
                    //schedule.EndTime = endTime;
                    schedule.Description = model.Description;
                    scheduleService.Create(schedule);
                    scheduleService.Save();

                    FieldScheduleViewModel result = PrepareFieldScheduleViewModel(schedule);

                    response = new ResponseModel <FieldScheduleViewModel>(true, "Lịch đã được tạo.", null, result);
                }
                else
                {
                    response = ResponseModel <FieldScheduleViewModel> .CreateErrorResponse("Tạo lịch thất bại", "Khung giờ đã tồn tại");
                }
            }
            catch (Exception)
            {
                response = ResponseModel <FieldScheduleViewModel> .CreateErrorResponse("Tạo lịch thất bại", systemError);
            }
            return(Json(response));
        }
Ejemplo n.º 3
0
        public ActionResult Update(int id, int placeId)
        {
            string        userID          = User.Identity.GetUserId();
            var           scheduleService = this.Service <IFieldScheduleService>();
            FieldSchedule schedule        = scheduleService.Get(id);
            CreateFieldScheduleViewModel updateSchedule;

            var          _placeService = this.Service <IPlaceService>();
            var          _fieldService = this.Service <IFieldService>();
            List <Place> listPlace     = new List <Place>();
            List <Field> listField     = new List <Field>();

            listPlace = _placeService.GetActive(p => p.UserId == userID).ToList();
            foreach (var item in listPlace)
            {
                if (item.Id == placeId)
                {
                    listField.AddRange(_fieldService.GetActive(x => x.PlaceId == item.Id).ToList());
                }
            }
            List <SelectListItem> selectListField = new List <SelectListItem>();

            foreach (var item in listField)
            {
                selectListField.Add(new SelectListItem {
                    Value = item.Id.ToString(), Text = item.Name
                });
            }

            List <SelectListItem> scheduleType = new List <SelectListItem>();

            foreach (FieldScheduleStatus item in Enum.GetValues(typeof(FieldScheduleStatus)))
            {
                scheduleType.Add(new SelectListItem {
                    Value = Convert.ToString((int)item), Text = Utils.GetEnumDescription(item)
                });
            }
            ViewBag.scheduleType    = scheduleType;
            ViewBag.selectListField = selectListField;

            //DateTime startDay = new DateTime(schedule.StartTime.Year, schedule.StartTime.Month, schedule.StartTime.Day);
            //DateTime endDay = new DateTime(schedule.EndTime.Year, schedule.EndTime.Month, schedule.EndTime.Day);
            //TimeSpan startTime = new TimeSpan(schedule.StartTime.Hour, schedule.StartTime.Minute, schedule.StartTime.Second);
            //TimeSpan endTime = new TimeSpan(schedule.EndTime.Hour, schedule.EndTime.Minute, schedule.EndTime.Second);

            if (schedule == null)
            {
                return(this.IdNotFound());
            }
            else
            {
                updateSchedule = Mapper.Map <CreateFieldScheduleViewModel>(schedule);
                updateSchedule.StartDateStr = updateSchedule.StartDate.ToString("dd/MM/yyyy");
                updateSchedule.EndDateStr   = updateSchedule.EndDate.ToString("dd/MM/yyyy");
                string start = updateSchedule.StartTime.ToString();
                updateSchedule.StartTimeStr = start.Substring(0, start.Length - 3);
                string end = updateSchedule.EndTime.ToString();
                updateSchedule.EndTimeStr = end.Substring(0, end.Length - 3);
                int    repeat = updateSchedule.AvailableDay;
                int    i      = 0;
                string s      = "";
                while (repeat > 0)
                {
                    if ((repeat & 1) == 1)
                    {
                        s += "," + i;
                    }

                    i++;
                    repeat >>= 1;
                }
                updateSchedule.Days    = s;
                updateSchedule.PlaceId = placeId;
                if (schedule.UserId != null)
                {
                    updateSchedule.UserName = schedule.AspNetUser.Email;
                }
            }
            //ViewBag.startDay = startDay;
            //ViewBag.endDay = endDay;
            //ViewBag.startTime = startTime;
            //ViewBag.endTime = endTime;
            return(this.PartialView(updateSchedule));
        }
Ejemplo n.º 4
0
        public ActionResult FieldScheduleOrderDetail(int id)
        {
            var           _placeService         = this.Service <IPlaceService>();
            var           _fieldService         = this.Service <IFieldService>();
            var           _fieldScheduleService = this.Service <IFieldScheduleService>();
            FieldSchedule fs      = _fieldScheduleService.FirstOrDefault(o => o.Id == id);
            var           fieldId = fs.FieldId;
            var           field   = _fieldService.FirstOrDefaultActive(p => p.Id == fieldId);
            Place         place   = new Place();

            if (field != null)
            {
                place         = _placeService.FirstOrDefaultActive(p => p.Id == field.PlaceId);
                ViewBag.field = field;
            }
            if (place != null)
            {
                ViewBag.place = place;
            }
            FieldScheduleViewModel model = new FieldScheduleViewModel(fs);

            model.StartTimeString = model.StartTime.Hours.ToString("00") + ":" + model.StartTime.Minutes.ToString("00");
            model.EndTimeString   = model.EndTime.Hours.ToString("00") + ":" + model.EndTime.Minutes.ToString("00");
            var bits = new bool[8];

            for (var i = 7; i >= 0; i--)
            {
                bits[i] = (model.AvailableDay & (1 << i)) != 0;
            }
            var dayOfWeek = "";

            if (bits[1])
            {
                dayOfWeek += "CN ";
            }
            if (bits[2])
            {
                dayOfWeek += "T2 ";
            }
            if (bits[3])
            {
                dayOfWeek += "T3 ";
            }
            if (bits[4])
            {
                dayOfWeek += "T4 ";
            }
            if (bits[5])
            {
                dayOfWeek += "T5 ";
            }
            if (bits[6])
            {
                dayOfWeek += "T6 ";
            }
            if (bits[7])
            {
                dayOfWeek += "T7 ";
            }
            model.availableDayOfWeek = dayOfWeek;
            return(this.PartialView(model));
        }
        //private void PrepareFieldScheduleViewModel(FieldScheduleViewModel schedule) {
        //    schedule.EndTimeString = schedule.EndTime.ToString("dd/MM/yyyy HH:mm:ss");

        //    schedule.StartTimeString = schedule.StartTime.ToString("dd/MM/yyyy HH:mm:ss");

        //    schedule.TypeString = Utils.GetEnumDescription((FieldScheduleStatus)schedule.Type);
        //}

        private LoadFieldScheduleViewModel PrepareFieldScheduleViewModel(FieldSchedule schedule)
        {
            LoadFieldScheduleViewModel result = Mapper.Map <LoadFieldScheduleViewModel>(schedule);

            result.StartDateStr = schedule.StartDate.ToString("dd/MM/yyyy");

            result.EndDateStr   = schedule.EndDate.ToString("dd/MM/yyyy");
            result.StartTimeStr = schedule.StartTime.ToString();
            result.EndTimeStr   = schedule.EndTime.ToString();
            int    repeat = schedule.AvailableDay;
            int    i      = 0;
            string s      = "";

            while (repeat > 0)
            {
                if ((repeat & 1) == 1)
                {
                    switch (i)
                    {
                    case 1:
                        s += "Chủ nhật - ";
                        break;

                    case 2:
                        s += "Thứ hai - ";
                        break;

                    case 3:
                        s += "Thứ ba - ";
                        break;

                    case 4:
                        s += "Thứ tư - ";
                        break;

                    case 5:
                        s += "Thứ năm - ";
                        break;

                    case 6:
                        s += "Thứ sáu - ";
                        break;

                    case 7:
                        s += "Thứ bảy - ";
                        break;
                    }
                }

                i++;
                repeat >>= 1;
            }
            s                 = s.Substring(0, s.Length - 3);
            result.Days       = s;
            result.TypeString = Utils.GetEnumDescription((FieldScheduleStatus)schedule.Type);

            if (schedule.Field != null)
            {
                result.FieldName = schedule.Field.Name;
            }

            return(result);
        }