private AnimalResponse SendRequest(AnimalRequest request) { string jsonRequest = JsonConvert.SerializeObject(request); byte[] msg = Encoding.UTF8.GetBytes(jsonRequest); // Отправляем данные через сокет sendSocket.Send(msg); // Получаем ответ от сервера int bytesRec = sendSocket.Receive(bytes); var json = Encoding.UTF8.GetString(bytes, 0, bytesRec); var response = new AnimalResponse { IsSuccess = false }; try { response = JsonConvert.DeserializeObject <AnimalResponse>(json); } catch (Exception exception) { MessageBox.Show(exception.Message + "\nНе удалось десериализовать", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } return(response); }
// Mappers // Note: doesn't expose behavior public static PetResponse FromPetDTO(PetDTO item) => new PetResponse() { Id = item.Id, Animal = AnimalResponse.FromAnimalDTO(item.Animal), Name = item.Name, Owner = EmployeeResponse.FromEmployeeDTO(item.Employee) };
/// <summary> /// Retrieves the animals per category /// </summary> /// <param name="categorId">The categoryId</param> /// <returns>Details of animals per category</returns> public AnimalResponse GetAnimalsPercategory(string categorId) { AnimalResponse animalResponse = new AnimalResponse(); List <Animal> animals = animalRepo.RetrieveAnimalDetailsPerCategory(categorId); animalResponse.animalList = animals; return(animalResponse); }
/// <summary> /// Retrieves all the animals /// </summary> /// <returns>All the animals</returns> public AnimalResponse GetAllAnimals() { AnimalResponse animalResponse = new AnimalResponse(); List <Animal> animals = animalRepo.RetrieveAllAnimals(); animalResponse.animalList = animals; return(animalResponse); }
/// <summary> /// Retrieves the animal details per category /// </summary> /// <param name="fromDate">The from date</param> /// <param name="toDate">The end date</param> /// <returns>Details of count of animals per category over the duration</returns> public AnimalResponse GetAnimalsCountPerCategory(string fromDate, string toDate) { AnimalResponse animalResponse = new AnimalResponse(); List <AnimalCount> count = animalRepo.RetrieveAnimalsCountPerCategory(fromDate, toDate); animalResponse.totalAnimalDetails = count; return(animalResponse); }
public void UpdateAnimalTest() { AnimalService service = new AnimalService(); animal.Setup(t => t.UpdateAnimal(dataSet.getAnimalDataSet())).Returns(dataSet.getAnimalDataSet()); AnimalResponse response = service.UpdateAnimal(dataSet.getAnimalDataSet()); Assert.IsTrue(response.animal.animalId == 1); }
public void GetAllAnimalsTest() { AnimalService service = new AnimalService(); animal.Setup(t => t.RetrieveAllAnimals()).Returns(dataSet.getAnimalListDataSet()); AnimalResponse response = service.GetAllAnimals(); Assert.IsTrue(response.animalList.Count > 0); }
public void AddNewAnimalTest() { AnimalService service = new AnimalService(); animal.Setup(t => t.CreateNewAnimal(dataSet.getAnimalDataSet())).Returns(dataSet.getAnimalDataSet()); AnimalResponse response = service.AddAnimal(dataSet.getAnimalDataSet()); Assert.AreEqual("1", response.animal.animalId); }
/// <summary> /// Updates an animal details /// </summary> /// <param name="animalDetails">The animal details</param> /// <returns>Details of updated animal</returns> public AnimalResponse UpdateAnimal(Animal animalDetails) { AnimalResponse animalResponse = new AnimalResponse(); Animal animal = animalRepo.UpdateAnimal(animalDetails); animalResponse.animal = animal; animalResponse.message = "Animal is updated successfully"; return(animalResponse); }
/// <summary> /// Deletes the animals /// </summary> /// <param name="animalId">The animal id</param> /// <returns>Details of animals</returns> public AnimalResponse DeleteAnimal(string animalId) { AnimalResponse animalResponse = new AnimalResponse(); Animal animal = animalRepo.DeleteAnimal(animalId); animalResponse.animal = animal; animalResponse.message = "Animal is deleted successfully"; return(animalResponse); }
/// <summary> /// Add the animals to the GPS device /// </summary> /// <param name="animalDetails">The animal details</param> /// <returns>The details of animal that is added</returns> public AnimalResponse AddAnimal(Animal animalDetails) { AnimalResponse animalResponse = new AnimalResponse(); Animal animal = animalRepo.CreateNewAnimal(animalDetails); animalResponse.animal = animal; animalResponse.message = "Animal is allocated successfully"; return(animalResponse); }
public IActionResult Create([FromBody] CreateAnimalRequest newAnimal) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var animal = _animals.Create(newAnimal); var species = _species.GetBySpecies(newAnimal.SpeciesType); //var url = Url.Action("GetById", new { id = animal.AnimalId }); var responseViewModel = new AnimalResponse(animal, species); return(Created("test", responseViewModel)); }
public IActionResult Create([FromBody] CreateAnimalRequest newAnimal) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Console.WriteLine(newAnimal.EnclosureName); var enclosure = _animals.GetByEnclosureName(newAnimal.EnclosureName); var animal = _animals.Create(newAnimal, enclosure); var url = Url.Action("GetById", new { id = animal.AnimalId }); var responseViewModel = new AnimalResponse(animal); return(Created(url, responseViewModel)); }
private static void Action(object o) { Socket socket = o as Socket; if (socket != null) { while (true) { try { // Мы дождались клиента, пытающегося с нами соединиться byte[] bytes = new byte[10240]; int bytesRec = socket.Receive(bytes); string json = Encoding.UTF8.GetString(bytes, 0, bytesRec); AnimalResponse response = new AnimalResponse { IsSuccess = false }; try { var request = JsonSerializer.Deserialize <AnimalRequest>(json); if (request != null) { response.Key = request.Key; Animal Animal; switch (request.Type) { case AnimalRequestType.Get: if (_animals.TryGetValue(request.Key, out Animal)) { response.Animal = Animal; response.IsSuccess = true; } else { response.ErrorMessage = "Ключ не найден"; } break; case AnimalRequestType.Add: if (_animals.ContainsKey(request.Key)) { response.ErrorMessage = "Животное с таким ключем уже существует"; } else { _animals.AddOrUpdate(request.Key, request.Animal, (s, Animal1) => request.Animal); response.IsSuccess = true; } break; case AnimalRequestType.Update: if (_animals.ContainsKey(request.Key)) { _animals.AddOrUpdate(request.Key, request.Animal, (s, Animal1) => request.Animal); response.IsSuccess = true; } else { response.ErrorMessage = "Животное с таким ключем не существует"; } break; case AnimalRequestType.Remove: if (_animals.ContainsKey(request.Key)) { if (_animals.TryRemove(request.Key, out Animal)) { response.IsSuccess = true; } else { response.ErrorMessage = "Не удалось удалить животное"; } } else { response.ErrorMessage = "Животное с таким ключем не существует"; } break; default: throw new ArgumentOutOfRangeException(); } } } catch (Exception exception) { response.ErrorMessage = exception.Message; } // Показываем данные на консоли Console.WriteLine("Полученный json: " + json); // Отправляем ответ клиенту\ var jsonResponse = JsonSerializer.Serialize <AnimalResponse>(response); byte[] msg = Encoding.UTF8.GetBytes(jsonResponse); Console.Write("Отправленный json: " + jsonResponse); socket.Send(msg); } catch (Exception) { break; } } socket.Shutdown(SocketShutdown.Both); socket.Close(); } }