public async Task <bool> CreatePet(PetVm pet) { var newPet = new Pet { Name = pet.Name, Latitude = pet.Latitude, Longitude = pet.Longitude, BreedId = pet.Breed.Id, PetTypeId = pet.Type.Id }; _repo.Pets.Add(newPet); return(await _repo.SaveChangesAsync() > 0); }
public async Task <bool> CreatePet(PetVm pet) { await Task.Delay(1000); var newPet = new Pet { Name = pet.Name, Latitude = pet.Latitude, Longitude = pet.Longitude, BreedId = pet.Breed.Id, PetTypeId = pet.Type.Id }; Pets.Add(newPet); return(true); }
public async Task CreatePetReturnsMessage() { var newPet = new PetVm { Name = "Keffas", Latitude = 50.123M, Longitude = -112.3553M, Breed = new Breed { ImgUrl = "//src/img", Name = "terrier", Id = 2 }, Type = new PetType { Id = 1, Name = "Dog" }, Location = "Regina, SK" }; var repo = new FakeModelRepo(); var repoMock = new Mock <IPetRepository>(); var logger = new Mock <ILogger>(); logger.Setup(x => x.Path).Returns("sample"); repoMock.Setup(x => x.ConfirmUniqueName(newPet.Name, newPet.Breed.Id)).Returns(repo.ConfirmUniqueName(newPet.Name, newPet.Breed.Id)); repoMock.Setup(x => x.CreatePet(newPet)).Returns(repo.CreatePet(newPet)); var controller = new PetController(repoMock.Object, logger.Object); var result = await controller.CreatePet(newPet) as CreatedResult; newPet.Name = "Seyi"; var exists = await controller.CreatePet(newPet) as OkObjectResult; if (result != null) { var obj = JsonConvert.DeserializeObject <Result>(JsonConvert.SerializeObject(result.Value)); Assert.Equal("success", obj.Status); } if (exists != null) { var existsObj = JsonConvert.DeserializeObject <Result>(JsonConvert.SerializeObject(exists.Value)); Assert.Equal("exists", existsObj.Status); } }
public async Task <IActionResult> CreatePet([FromBody] PetVm pet) { try { var unique = await _repo.ConfirmUniqueName(pet.Name, pet.Breed.Id); if (!unique) { return(Ok(new { status = "exists", message = $"{pet.Name} already exists on the {pet.Breed.Name} breed. Choose another name" })); } if (await _repo.CreatePet(pet)) { return(Created("", new { status = "success", message = "Pet Created Successfully!" })); } return(Ok(new { status = "failed", message = "Could not create Pet" })); } catch (Exception ex) { _log.LogException(ex); return(BadRequest(new { status = "failed", message = "An error has occured. Kindly check back again" })); } }