public bool UpdateHealthFacility(int id, [FromBody] CreateHealthFacitityCommand request) { HealthFacilityContext context = HttpContext.RequestServices.GetService(typeof(HealthFacilityContext)) as HealthFacilityContext; UpdateHealthFacilityHandler handler = new UpdateHealthFacilityHandler(context); return(handler.Handle(id, request)); }
public bool Handle(int healthFacilityId, CreateHealthFacitityCommand request) { var model = request.Adapt <Model.MedicalHealthFacility>(); using (MySqlConnection conn = _context.GetConnection()) { conn.Open(); string query = string.Format("update health_facilities set name = '{1}', address= '{2}' where faculty_id={0}", healthFacilityId.ToString(), model.Name, Converter.ConvertToString(model.Address)); MySqlCommand cmd = new MySqlCommand(query, conn); try { cmd.ExecuteNonQuery(); } catch { return(false); } finally { conn.CloseAsync(); } } return(true); }
public bool Handle(CreateHealthFacitityCommand request) { var model = request.Adapt <MedicalHealthFacility>(); string address = string.Format("улица {0}, дом {1}, город {2}, страна {3}", model.Address.Street, model.Address.House, model.Address.City, model.Address.Country); using (MySqlConnection conn = _context.GetConnection()) { conn.Open(); string query = string.Format("insert into Health_Facilities(name, address) values('{0}', '{1}')", model.Name, address); MySqlCommand cmd = new MySqlCommand(query, conn); try { cmd.ExecuteNonQuery(); } catch { return(false); } finally { conn.CloseAsync(); } } return(true); }
public void UpdateFacilityTest() { // arrange List <MedicalHealthFacility> list = new List <MedicalHealthFacility>(); int id = 2; MedicalHealthFacility expected = new MedicalHealthFacility { Name = "Facility#3", Address = new Address { Street = "s", City = "c", Country = "c", House = 1 }, }; CreateHealthFacitityCommand command = new CreateHealthFacitityCommand { Name = "Facility#3", Address = new Address { Street = "street", City = "city", Country = "country", House = 11 }, }; //act HealthFacilityContext context = new HealthFacilityContext(connString); UpdateHealthFacilityHandler handler = new UpdateHealthFacilityHandler(context); handler.Handle(2, command); using (conn = new MySqlConnection(connString)) { conn.Open(); string query = string.Format("select * from health_facilities where address='улица street, дом 11, город city, страна country' and name='Facility#3'"); MySqlCommand cmd = new MySqlCommand(query, conn); using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { string addressString = reader["address"].ToString(); Address address = Converter.ConvertToAddress(addressString); list.Add(new MedicalHealthFacility() { Id = Convert.ToInt32(reader["faculty_id"]), Name = reader["name"].ToString(), Address = address, }); } } } if (list[0].Name == expected.Name && list[0].Address == expected.Address && list[0].Id == id) { Assert.IsTrue(true); } }