public Task <Stream> ShowPetImagePoco(string petId) { long.TryParse(petId, out long idAsLong); PetResource pet = this.pets.SingleOrDefault(p => p.Id == idAsLong); return(this.GetImageForPetAsync(pet)); }
/// <summary> /// Initializes a new instance of the <see cref="PetStoreService"/> class. /// </summary> /// <param name="petListMapper">Mapper for pet list responses.</param> /// <param name="petMapper">Mapper for pet responses.</param> /// <param name="httpClientFactory">Source of <c>HttpClient</c>s.</param> public PetStoreService( PetListResourceMapper petListMapper, PetResourceMapper petMapper, IHttpClientFactory httpClientFactory) { this.pets = new List <PetResource> { new PetResource("Suki", "Cat") { Id = 1, Size = Size.Small, GlobalIdentifier = Guid.NewGuid() }, new PetResource("Caspar", "Dog") { Id = 2, Size = Size.Medium, GlobalIdentifier = Guid.NewGuid() }, new PetResource("Tim", "Goldfish") { Id = 3, Size = Size.Large, GlobalIdentifier = Guid.NewGuid() }, }; this.petListMapper = petListMapper; this.petMapper = petMapper; this.httpClientFactory = httpClientFactory; this.secretPet = new PetResource("Yogi", "Bear") { Id = 0, Size = Size.Large, GlobalIdentifier = Guid.NewGuid() }; }
public Task <OpenApiResult> ShowPetWithIdInHeader( long xPetId) { PetResource result = this.pets.SingleOrDefault(p => p.Id == xPetId); return(this.MapAndReturnPetAsync(result)); }
public Task <OpenApiResult> ShowPet( [OpenApiParameter("petId")] long petIdWithParameterNameSetByAttribute) { PetResource result = this.pets.SingleOrDefault(p => p.Id == petIdWithParameterNameSetByAttribute); return(this.MapAndReturnPetAsync(result)); }
public Task <OpenApiResult> ShowPet(string petId) { long.TryParse(petId, out long idAsLong); PetResource result = this.pets.SingleOrDefault(p => p.Id == idAsLong); return(this.MapAndReturnPetAsync(result)); }
public async Task <ActionResult <Pet> > AddPet([FromBody] PetResource pet) { List <Caretaker> caretakers = new List <Caretaker>(); foreach (long id in pet.caretakerIds) { var caretaker = await this.caretakerRepository.GetAsync(id); if (caretaker == null) { return(NotFound($"Caretaker with id {id} Not Found")); } caretakers.Add(caretaker); } var vet = await this.vetRepository.GetAsync(pet.vetId); if (vet == null) { return(NotFound("Vet Not Found")); } Pet newPet = new Pet { name = pet.name, age = pet.age, vet = vet, caretakers = caretakers }; this.petRepository.Add(newPet); await this.unitOfWork.CompletedAsync(); return(Ok(newPet)); }
private async Task <Stream> GetImageForPetAsync(PetResource pet) { using HttpClient client = this.httpClientFactory.CreateClient(); string uri = $"https://loremflickr.com/320/240/{HttpUtility.UrlEncode(pet.Tag)},{pet.Size}"; byte[] image = await client.GetByteArrayAsync(uri).ConfigureAwait(false); return(new MemoryStream(image)); }
public async Task <OpenApiResult> ShowPetImage(string petId) { long.TryParse(petId, out long idAsLong); PetResource pet = this.pets.SingleOrDefault(p => p.Id == idAsLong); Stream result = await this.GetImageForPetAsync(pet).ConfigureAwait(false); return(this.OkResult(result, "image/jpeg")); }
private async Task <OpenApiResult> MapAndReturnPetAsync(PetResource result) { if (result == null) { throw new OpenApiNotFoundException(); } HalDocument response = await this.petMapper.MapAsync(result).ConfigureAwait(false); return(this .OkResult(response, "application/hal+json") .WithAuditData(("id", (object)result.Id))); }
public async Task <OpenApiResult> CreatePets(PetResource body) { if (!this.pets.Any(p => p.Id == body.Id)) { this.pets.Add(body); } HalDocument response = await this.petMapper.MapAsync(body).ConfigureAwait(false); WebLink location = response.GetLinksForRelation("self").First(); return(this.CreatedResult(location.Href, response).WithAuditData(("id", (object)body.Id))); }
public Task <OpenApiResult> ShowPet(Guid petId) { PetResource result = this.pets.SingleOrDefault(p => p.GlobalIdentifier == petId); return(this.MapAndReturnPetAsync(result)); }