public IActionResult Create(CreateStationInputModel model) { if (!ModelState.IsValid) { return(this.View(model)); } var result = this.StationService.CreateStation(model); if (!result.HasError) { this.TempData.Put(MessageConstants.Name, new MessageModel { Type = MessageType.Success, Message = result.Message }); } else { this.TempData.Put(MessageConstants.Name, new MessageModel { Type = MessageType.Danger, Message = result.Message }); return(this.RedirectToAction(nameof(Create))); } return(this.RedirectToAction(nameof(Index))); }
public BaseModel CreateStation(CreateStationInputModel model) { var hasStationName = this.Db.Stations.Any(x => x.StationName == model.StationName); var hasStationCode = this.Db.Stations.Any(x => x.StationCode == model.StationCode); if (hasStationName) { this.BaseModel.HasError = true; this.BaseModel.Message = string.Format(MessageConstants.HaveStationName, model.StationName); return(this.BaseModel); } if (hasStationCode) { this.BaseModel.HasError = true; this.BaseModel.Message = string.Format(MessageConstants.HaveStationCode, model.StationCode); return(this.BaseModel); } var station = new Station { StationName = model.StationName, StationCode = model.StationCode, }; try { this.Db.Stations.Add(station); this.Db.SaveChanges(); this.BaseModel.HasError = false; this.BaseModel.Message = MessageConstants.CreateStation; } catch (Exception) { this.BaseModel.HasError = true; this.BaseModel.Message = MessageConstants.NoCreateStation; } return(this.BaseModel); }