public IEnumerable <IUnitsModel> GetAll() { List <UnitsModel> units = new List <UnitsModel>(); string query = "select * from Units"; using (var db = new SqlCeConnection(connectionString)) { db.Open(); using (SqlCeCommand command = new SqlCeCommand(query, db)) { using (SqlCeDataReader reader = command.ExecuteReader()) { while (reader.Read()) { UnitsModel unit = new UnitsModel { Id = Convert.ToInt32(reader["Id"]), Name = reader["Name"].ToString(), Notes = reader["Notes"].ToString() }; units.Add(unit); } } } db.Close(); } return(units); }
public IUnitsModel GetById(int id) { UnitsModel unit = new UnitsModel(); using (var db = new SqlCeConnection(connectionString)) { db.Open(); string query = "select Id, Name, Notes from Units where Id=@Id"; using (SqlCeCommand cmd = new SqlCeCommand(query, db)) { cmd.Prepare(); cmd.Parameters.AddWithValue("@Id", id); using (SqlCeDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { unit.Id = Convert.ToInt32(reader["Id"]); unit.Name = reader["Name"].ToString(); unit.Notes = reader["Notes"].ToString(); } } } db.Close(); } return(unit); }
public void Update_ShouldReturn_Success() { // Arrange UnitsModel unit = new UnitsModel { Id = 1, Name = "g", Notes = "gram" }; fakeUnitsRepository.Setup(a => a.Update(unit)); unitsService = new UnitsService(fakeUnitsRepository.Object); UnitsDtoModel unitDto = new UnitsDtoModel { Id = 1, Name = "g", Notes = "updated notes" }; try { // Act unitsService.UpdateUnit(unitDto); operationSucceeded = true; } catch (Exception ex) { errorMessage = ex.Message + " | " + ex.StackTrace; } // Assert Assert.IsTrue(operationSucceeded, errorMessage); }
public void AddUnit_ShouldReturn_Success() { // Arrange UnitsModel unit = new UnitsModel { Name = "g", Notes = "gram" }; fakeUnitsRepository.Setup(a => a.Add(unit)); unitsService = new UnitsService(fakeUnitsRepository.Object); try { // Act unitsService.AddUnit(new UnitsDtoModel { Name = unit.Name, Notes = unit.Notes }); operationSucceeded = true; } catch (Exception ex) { errorMessage = ex.Message + " | " + ex.StackTrace; } // Assert Assert.IsTrue(operationSucceeded, errorMessage); }
public ActionResult Edit(UnitsModel modifiedModel, string FILTER_Keyword, int?FILTER_Active) { if (ModelState.IsValid) { if (isExists(modifiedModel.Id, modifiedModel.Name)) { ModelState.AddModelError(UnitsModel.COL_Name.Name, $"{modifiedModel.Name} sudah terdaftar"); } else { UnitsModel originalModel = db.Units.AsNoTracking().Where(x => x.Id == modifiedModel.Id).FirstOrDefault(); string log = string.Empty; log = Helper.append(log, originalModel.Name, modifiedModel.Name, UnitsModel.COL_Name.LogDisplay); log = Helper.append(log, originalModel.Active, modifiedModel.Active, UnitsModel.COL_Active.LogDisplay); if (!string.IsNullOrEmpty(log)) { db.Entry(modifiedModel).State = EntityState.Modified; ActivityLogsController.AddEditLog(db, Session, modifiedModel.Id, log); db.SaveChanges(); } return(RedirectToAction(nameof(Index), new { FILTER_Keyword = FILTER_Keyword, FILTER_Active = FILTER_Active })); } } setViewBag(FILTER_Keyword, FILTER_Active); return(View(modifiedModel)); }
public ActionResult UnitsAdd(UnitsModel model) { if (Session["UserTicket"] != null) { model.addUnit(model, new Guid(Session["UserTicket"].ToString())); return(Redirect("~/Units/Units")); } return(Redirect("~/Login/Login")); }
public ActionResult UnitsAdd() { if (Session["UserTicket"] != null && Session["UserId"] != null) { UnitsModel model = new UnitsModel(); return(View(model)); } return(Redirect("~/Login/Login")); }
// GET: Units public ViewResult Units(string sortOrder, string currentFilter, string searchString, int?page) { if (Session["UserTicket"] != null) { ViewBag.CurrentSort = sortOrder; ViewBag.IdSortParm = String.IsNullOrEmpty(sortOrder) ? "id_asc" : ""; ViewBag.NameSortParm = sortOrder == "Name" ? "Name_desc" : "Name"; if (searchString != null) { page = 1; } else { searchString = currentFilter; } ViewBag.CurrentFilter = searchString; UnitsModel m = new UnitsModel(); var units = m.getUnits(new Guid(Session["UserTicket"].ToString())); if (!String.IsNullOrEmpty(searchString)) { units = units.Where(s => s.Name.Contains(searchString) || s.Id.ToString().Contains(searchString) || s.Description.Contains(searchString)); } switch (sortOrder) { case "id_asc": units = units.OrderByDescending(s => s.Id); break; case "Name": units = units.OrderBy(s => s.Name); break; case "Name_desc": units = units.OrderByDescending(s => s.Name); break; default: // id ascending units = units.OrderBy(s => s.Id); break; } int pageSize = 6; int pageNumber = (page ?? 1); return(View(units.ToPagedList(pageNumber, pageSize))); } RedirectToAction("Login", "login"); return(View()); }
/// <summary> /// Оновлює одиницю виміру /// </summary> /// <param name="unitDto">Екземпляр одиниці виміру</param> public void UpdateUnit(UnitsDtoModel unitDto) { var mapper = new MapperConfiguration(cfg => cfg.CreateMap <UnitsDtoModel, UnitsModel>()).CreateMapper(); UnitsModel unit = mapper.Map <UnitsModel>(unitDto); var results = unitsValidator.Validate(unit); if (results.IsValid) { unitsRepository.Update(unit); } else { throw new Exception("Помилка валідації одиниці виміру: " + Environment.NewLine + ValidationResultsHelper.GetValidationErrors(results)); } }
public ActionResult UnitsEdit(int id) { if (Session["UserTicket"] != null) { DataQueryService.IDataQueryService dataQueryService = new DataQueryService.DataQueryServiceClient(); var unit = dataQueryService.GetUnit(id, new Guid(Session["UserTicket"].ToString())); UnitsModel model = new UnitsModel(); model.Id = unit.Id; model.Name = unit.Name; model.Description = unit.Description; return(View(model)); } return(Redirect("~/Login/Login")); }
/// <summary> /// Повертає список усіх одиниць виміру /// </summary> /// <returns>Список одиниць виміру</returns> public IEnumerable <IUnitsModel> GetAll() { List <UnitsModel> units = new List <UnitsModel>(); using (var db = new SqlCeConnection(connectionString)) { try { db.Open(); } catch (SqlCeException) { throw new Exception("Немає підключення до бази даних."); } string query = "select * from Units"; using (SqlCeCommand command = new SqlCeCommand(query, db)) { try { using (SqlCeDataReader reader = command.ExecuteReader()) { while (reader.Read()) { UnitsModel unit = new UnitsModel { Id = Convert.ToInt32(reader["Id"]), Name = reader["Name"].ToString(), Notes = reader["Notes"].ToString() }; units.Add(unit); } } } catch (SqlCeException) { throw new Exception("Помилка отримання списку одиниць виміру з бази даних."); } } } return(units); }
/// <summary> /// Повертає одиницю виміру по ідентифікатору /// </summary> /// <param name="id">Ідентифікатор одиниці виміру</param> /// <returns>Екземпляр одиниці виміру</returns> public IUnitsModel GetById(int id) { UnitsModel unit = new UnitsModel(); using (var db = new SqlCeConnection(connectionString)) { try { db.Open(); } catch (SqlCeException) { throw new Exception("Немає підключення до бази даних."); } string query = "select Id, Name, Notes from Units where Id=@Id"; using (SqlCeCommand cmd = new SqlCeCommand(query, db)) { cmd.Prepare(); cmd.Parameters.AddWithValue("@Id", id); try { using (SqlCeDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { unit.Id = Convert.ToInt32(reader["Id"]); unit.Name = reader["Name"].ToString(); unit.Notes = reader["Notes"].ToString(); } } } catch (SqlCeException) { throw new Exception("Помилка отримання одиниці виміру з бази даних."); } } } return(unit); }
public ActionResult Create(UnitsModel model, string FILTER_Keyword, int?FILTER_Active) { if (ModelState.IsValid) { if (isExists(null, model.Name)) { ModelState.AddModelError(UnitsModel.COL_Name.Name, $"{model.Name} sudah terdaftar"); } else { model.Id = Guid.NewGuid(); model.Active = true; db.Units.Add(model); ActivityLogsController.AddCreateLog(db, Session, model.Id); db.SaveChanges(); return(RedirectToAction(nameof(Index), new { id = model.Id, FILTER_Keyword = FILTER_Keyword, FILTER_Active = FILTER_Active })); } } setViewBag(FILTER_Keyword, FILTER_Active); return(View(model)); }