public async Task <Persona> Add(CreatePersonaInputModel persona) { Persona currentVersion = await db.Personas.AsQueryable() .Where(p => p.WikiId == persona.WikiId) .SingleOrDefaultAsync(); if (currentVersion != null) { throw new EntityAlreadyExistsException <Persona>(currentVersion); } Persona model = await scrappingService.ScrapePersonaDetails(persona.WikiId); model.AddedBy = PrincipalUsername; var fromDb = db.Add(model); await db.SaveChangesAsync(); return(fromDb.Entity); }
public async Task <ActionResult <Persona> > PostPersona(CreatePersonaInputModel persona) { try { Persona fromDb = await _personas.Add(persona); return(CreatedAtAction("GetPersona", new { id = fromDb.Id }, fromDb)); } catch (EntityAlreadyExistsException <Persona> ex) { return(Ok(ex.Entity)); } catch (AddedEntityIsNotHumanException ex) { return(BadRequest(new ErrorMessage(ex.Message))); } catch (EntityNotFoundException ex) { return(BadRequest(new ErrorMessage(ex.Message))); } }
public void Add_ThrowsIfPersonaExists() { var wikiId = db.Personas.First().WikiId; var newPersona = new CreatePersonaInputModel() { WikiId = wikiId }; mockScrapping.Setup(s => s.ScrapePersonaDetails(wikiId)).ReturnsAsync(new Persona() { WikiId = wikiId, Name = "John Doe", Description = "Not a real person", ImageUri = new Uri("https://SomeUrl.com"), WikipediaUri = new Uri("https://Wiki.com"), Recommendations = null, }); Service.Awaiting(s => s.Add(newPersona)) .Should().Throw <EntityAlreadyExistsException <Persona> >("because a persona with that wikiId already exists"); }
public async Task Add_AddsNewPersonaToDb() { var wikiId = "Q999999"; var newPersona = new CreatePersonaInputModel() { WikiId = wikiId }; var expectedPersona = new Persona() { WikiId = wikiId, Name = "John Doe", Description = "Not a real person", ImageUri = new Uri("https://SomeUrl.com"), WikipediaUri = new Uri("https://Wiki.com"), Recommendations = null, AddedBy = TestingUser, }; mockScrapping.Setup(s => s.ScrapePersonaDetails(wikiId)).ReturnsAsync(new Persona() { WikiId = wikiId, Name = "John Doe", Description = "Not a real person", ImageUri = new Uri("https://SomeUrl.com"), WikipediaUri = new Uri("https://Wiki.com"), Recommendations = null, }); var actual = await Service.Add(newPersona); actual.Should().BeEquivalentTo(expectedPersona, options => options.Excluding(p => p.Id)); db.Personas.AsQueryable().Where(p => p.WikiId == wikiId).FirstOrDefault() .Should().NotBeNull("because we want it to be saved in the database") .And.BeEquivalentTo(expectedPersona, options => options.Excluding(p => p.Id)); }