public AddGasStationResult AddGasStation([FromBody] GasStaionMV gasStationMV) { AddGasStationResult addGasStationResult = new AddGasStationResult(); List <Error> errors = new List <Error>(); if (!ModelState.IsValid || !_gasStationRepository.CheckIsNotExistName(gasStationMV.GasStationName)) { if (!_gasStationRepository.CheckIsNotExistName(gasStationMV.GasStationName)) { errors.Add(new Error("E0003", string.Format(Resources.Resource.E0003, gasStationMV.GasStationName))); } addGasStationResult.Errors = errors; addGasStationResult.Status = false; return(addGasStationResult); } GasStation gasStation = new GasStation(); try { gasStation.GasStationName = gasStationMV.GasStationName; gasStation.District = gasStationMV.District.DistrictId; gasStation.OpeningTime = gasStationMV.OpeningTime; gasStation.Address = gasStationMV.Address; gasStation.Longitude = gasStationMV.Longitude; gasStation.Latitude = gasStationMV.Latitude; gasStation.Rating = gasStationMV.Rating.Code; gasStation.InsertedAt = DateTime.Now; gasStation.InsertedBy = Convert.ToInt64(HttpContext.Session.GetString("userId")); gasStation.UpdatedAt = DateTime.Now; gasStation.UpdatedBy = Convert.ToInt64(HttpContext.Session.GetString("userId")); _gasStationRepository.Insert(gasStation); _unitOfWork.Commit(); try { foreach (var item in gasStationMV.Types) { GasStationGasType gasStationGasType = new GasStationGasType(); gasStationGasType.GasStationId = gasStation.GasStationId; gasStationGasType.GasType = item.GasTypeCode; _gasStationGasTypeRepository.Insert(gasStationGasType); _unitOfWork.Commit(); } } catch { addGasStationResult.Status = false; } } catch { ModelState.AddModelError("", "Dữ liệu không hợp lệ, vui lòng kiểm tra lại"); addGasStationResult.Status = false; return(addGasStationResult); } addGasStationResult.GasStation = gasStation; addGasStationResult.Status = true; return(addGasStationResult); }
public ActionResult Add(GasStationAddVM gasStationAddVM) { ViewBag.listDistrist = _districtRepository.GetAll().OrderBy(x => x.DistrictName); ViewBag.listGasType = _mTpyeRepository.GetAll().Where(x => x.TypeType == 3).ToList(); ViewBag.ratingList = _mTpyeRepository.GetAll().Where(x => x.TypeType == 4).ToList(); if (ModelState.IsValid) { if (_gasStationRepository.isNameExist(gasStationAddVM.GasStationName)) { ModelState.AddModelError("", Resources.Resource.E0003); return(View()); } DataAccess.Models.GasStation gasStation = new DataAccess.Models.GasStation(); try { gasStation.GasStationName = gasStationAddVM.GasStationName; gasStation.Address = gasStationAddVM.Address; gasStation.OpeningTime = gasStationAddVM.OpeningTime; gasStation.District = Convert.ToInt64(gasStationAddVM.District); gasStation.Latitude = Convert.ToDouble(gasStationAddVM.Latitude); gasStation.Longitude = Convert.ToDouble(gasStationAddVM.Longitude); gasStation.Rating = gasStationAddVM.Rating; gasStation.InsertedAt = DateTime.Now; string userID = Session["個人ID"].ToString().Replace("{", "").Replace("}", ""); gasStation.InsertedBy = Convert.ToInt64(Regex.Replace(userID, @"\D", "")); gasStation.UpdatedAt = DateTime.Now; gasStation.UpdatedBy = gasStation.InsertedBy; } catch { ModelState.AddModelError("", "Vui lòng kiểm tra lại."); return(View()); } _gasStationRepository.Insert(gasStation); _unitOfWork.Commit(); string[] listType = gasStationAddVM.Gatype.Split(Convert.ToChar(",")); foreach (var item in listType) { DataAccess.Models.GasStationGasType gasStationGasType = new DataAccess.Models.GasStationGasType(); gasStationGasType.GasStationId = gasStation.GasStationId; gasStationGasType.GasType = item.Trim(); _gasStationGasTypeRepository.Insert(gasStationGasType); } _unitOfWork.Commit(); return(RedirectToAction("Index", "Home")); } ModelState.AddModelError("", "Có lỗi, vui lòng kiểm tra lại"); return(View()); }