public static Place GenerateTestPlace(Guid id) { var place = new Place { Id = id, Title = "Test title " + _count++ }; return place; }
public static void AddPlace(Place place) { using (var client = new HttpClient { BaseAddress = new Uri("http://localhost:25889/") }) { client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var response = client.PostAsJsonAsync("api/places", place).Result; if (response.IsSuccessStatusCode) { MessageBox.Show("Place Added"); } else { MessageBox.Show("Error Code" + response.StatusCode + " : Message - " + response.ReasonPhrase); } } }
public void Create(Place place) { using (var connection = new SqlConnection(_connectionString)) { connection.Open(); using (var command = new SqlCommand("[dbo].[createPlace]", connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@Id", place.Id); command.Parameters.AddWithValue("@Title", place.Title); var returnValue = command.Parameters.Add("@Return", SqlDbType.Int); returnValue.Direction = ParameterDirection.ReturnValue; using (new LogWrapper("Adding place in database")) { command.ExecuteNonQuery(); } if ((int)returnValue.Value == Sqlerror) { _logger.Warn("Attempt to add place with existing title"); throw new ArgumentException("Place with this title already exists"); } } } }
public IEnumerable<Place> GetAllPlaces() { using (var connection = new SqlConnection(_connectionString)) { connection.Open(); using (var command = new SqlCommand("[dbo].[getAllPlaces]", connection)) { command.CommandType = CommandType.StoredProcedure; using (new LogWrapper("Getting all places from database")) { using (var reader = command.ExecuteReader()) { while (reader.Read()) { var place = new Place { Id = reader.GetGuid(reader.GetOrdinal("Id")), Title = reader.GetString(reader.GetOrdinal("Title")) }; yield return place; } } } } } }
//Проверяет, нужно ли вызывать PlacesHttpClient.UpdatePlace(updatedPlace); public void ProveThanUpdatePlace() { //Проверка на заполненность SelectedPlace if (SelectedPlace.Title == null) return; //Проверка на изменения if (UpdatedTitle.Equals(SelectedPlace.Title)) { MessageBox.Show("Nothing to update"); return; } var updatedPlace = new Place() { Title = UpdatedTitle, Id = SelectedPlace.Id }; PlacesHttpClient.UpdatePlace(updatedPlace); }
public PlacesViewModel() { Places = PlacesHttpClient.GetAllPlaces(); _selectedPlace = new Place(); }