/// <summary> /// Deletes a hairLengthLink by ID /// </summary> /// <param name="id">ID of the hairLengthLink to be deleted</param> /// <exception cref="ResourceNotFoundException"> /// Thrown if the user with the corresponding <seealso cref="id"/> is not found /// </exception> /// <returns>HairLengthLink deleted</returns> public async Task <HairLengthLinks> Delete(ulong id) { HairLengthLinks hairLengthLink = null; if (_context != null) { hairLengthLink = await _context.HairLengthLinks.FindAsync(id); if (hairLengthLink != null) { _context.HairLengthLinks.Remove(hairLengthLink); await _context.SaveChangesAsync(); } else { throw new ResourceNotFoundException("HairLengthLink not found"); } } else { hairLengthLink = HairLengthLinks.Where(u => u.Id == id).FirstOrDefault(); if (hairLengthLink != null) { HairLengthLinks.Remove(hairLengthLink); } else { throw new ResourceNotFoundException("HairLengthLink not found"); } } return(hairLengthLink); }
public async Task Edit_ReturnsTrue() { // Arrange _hairLengthLinksContext = _db.SeedHairLengthLinksContext(); ulong id = 2; List <HairLengthLinks> currentHairLengthLinks = _db.HairLengthLinks; HairLengthLinks current = currentHairLengthLinks.FirstOrDefault(c => c.Id == id); HairLengthLinks updated = current.ShallowCopy(); updated.LinkName = "medium links updated"; updated.LinkUrl = "https://i.pinimg.com/originals/56/16/79/56167980a30fa0d3bb098e7eeafdd411.jpg"; HairLengthLinks updatedHairLengthLink = new HairLengthLinks { Id = id, LinkName = updated.LinkName, LinkUrl = updated.LinkUrl, HairLengthId = updated.HairLengthId }; bool expected = true; // Act bool actual = await _hairLengthLinksContext.Edit(id, updatedHairLengthLink); HairLengthLinks u = _db.HairLengthLinks.FirstOrDefault(fs => fs.Id == id); _db.HairLengthLinks = new List <HairLengthLinks>(currentHairLengthLinks); // Assert Assert.Equal(expected, actual); Assert.Equal(updatedHairLengthLink.LinkUrl, u.LinkUrl); }
public async Task Add_ReturnsHairLengthLinkAdded() { // Arrange _hairLengthLinksContext = _db.SeedHairLengthLinksContext(); int currentHairLengthLinksCount = _db.HairLengthLinks.Count; List <HairLengthLinks> currentHairLengthLinks = _db.HairLengthLinks; HairLengthLinks expected = new HairLengthLinks { Id = 3, LinkName = "medium link 2", LinkUrl = "https://i2.wp.com/therighthairstyles.com/wp-content/uploads/2016/10/10-messy-curly-medium-length-bob.jpg?w=500&ssl=1", HairLengthId = 2 }; // Act HairLengthLinks actual = await _hairLengthLinksContext.Add(expected); int updatedHairLengthLinksCount = _db.HairLengthLinks.Count; _db.HairLengthLinks = new List <HairLengthLinks>(currentHairLengthLinks); // Assert Assert.Equal(expected.Id, actual.Id); Assert.Equal(currentHairLengthLinksCount + 1, updatedHairLengthLinksCount); }
public async Task Read_ReturnsHairLengthLinksById() { // Arrange _hairLengthLinksContext = _db.SeedHairLengthLinksContext(); ulong id = 1; HairLengthLinks expected = _db.HairLengthLinks.FirstOrDefault(c => c.Id == id); // Act HairLengthLinks actual = await _hairLengthLinksContext.ReadById(id); // Assert Assert.Equal(expected, actual); }
/// <summary> /// Adds a new hairLengthLink /// <para> /// NOTE: Currently this method is not restricted to unique hairLengthLinks, so the operation always succeeds /// </para> /// </summary> /// <param name="hairLengthLink">HairLengthLink to be added</param> /// <returns>HairLengthLink added</returns> public Task <HairLengthLinks> Add(HairLengthLinks hairLengthLink) { HairLengthLinks hairLengthLinkAdded = null; if (_context != null) { _context.HairLengthLinks.Add(hairLengthLink); hairLengthLinkAdded = hairLengthLink; } else { HairLengthLinks.Add(hairLengthLink); hairLengthLinkAdded = hairLengthLink; } return(Task.FromResult(hairLengthLinkAdded)); }
/// <summary> /// Updates an existing hairLengthLink /// </summary> /// <param name="id">ID of the hairLengthLink to be updated</param> /// <param name="updatedHairLengthLink">Updated hairLengthLink object</param> /// <exception cref="ResourceNotFoundException"> /// Thrown if <seealso cref="updatedHairLengthLink"/> does not exist /// </exception> /// <exception cref="DbUpdateConcurrencyException"> /// </exception> /// <returns>Result of the operation</returns> public async Task <bool> Edit(ulong id, HairLengthLinks updatedHairLengthLink) { bool hairLengthLinkUpdated = false; if (_context != null) { HairLengthLinks currentHairLengthLink = await _context.HairLengthLinks.FindAsync(id); try { if (currentHairLengthLink != null) { currentHairLengthLink.LinkName = updatedHairLengthLink.LinkName; currentHairLengthLink.LinkUrl = updatedHairLengthLink.LinkUrl; _context.Entry(currentHairLengthLink).State = EntityState.Modified; await _context.SaveChangesAsync(); hairLengthLinkUpdated = true; } throw new ResourceNotFoundException("HairLengthLink not found"); } catch (DbUpdateConcurrencyException ex) { throw ex; } } else { HairLengthLinks currentHairLengthLink = HairLengthLinks.FirstOrDefault(fs => fs.Id == id); if (currentHairLengthLink != null) { int currentHairLengthLinkIndex = HairLengthLinks.FindIndex(c => c.Id == id); HairLengthLinks[currentHairLengthLinkIndex] = updatedHairLengthLink; hairLengthLinkUpdated = true; } else { throw new ResourceNotFoundException("HairLengthLink not found"); } } return(hairLengthLinkUpdated); }
public async Task <ActionResult <HairLengthLinks> > PostHairLengthLinks([FromBody] HairLengthLinks hairLengthLinks) { if (!_authorizationService.ValidateJWTToken(Request)) { return(Unauthorized(new { errors = new { Token = new string[] { "Invalid token" } }, status = 401 })); } var correspondingHairLength = await _context.HairLengths.FirstOrDefaultAsync(h => h.Id == hairLengthLinks.HairLengthId); if (correspondingHairLength == null) { return(NotFound(new { errors = new { HairLengthId = new string[] { "No matching hair length entry was found" } }, status = 404 })); } _context.HairLengthLinks.Add(hairLengthLinks); await _context.SaveChangesAsync(); return(CreatedAtAction("GetHairLengthLinks", new { id = hairLengthLinks.Id }, hairLengthLinks)); }
public async Task <IActionResult> PutHairLengthLinks(ulong id, [FromBody] HairLengthLinks hairLengthLinks) { if (!_authorizationService.ValidateJWTToken(Request)) { return(Unauthorized(new { errors = new { Token = new string[] { "Invalid token" } }, status = 401 })); } if (id != hairLengthLinks.Id) { return(BadRequest(new { errors = new { Id = new string[] { "ID sent does not match the one in the endpoint" } }, status = 400 })); } var correspondingHairLength = await _context.HairLengths.FirstOrDefaultAsync(h => h.Id == hairLengthLinks.HairLengthId); if (correspondingHairLength == null) { return(NotFound(new { errors = new { HairLengthId = new string[] { "No matching hair length entry was found" } }, status = 404 })); } HairLengthLinks currentHLL = await _context.HairLengthLinks.FindAsync(id); try { if (currentHLL != null) { currentHLL.LinkName = hairLengthLinks.LinkName; currentHLL.LinkUrl = hairLengthLinks.LinkUrl; currentHLL.HairLengthId = hairLengthLinks.HairLengthId; _context.Entry(currentHLL).State = EntityState.Modified; await _context.SaveChangesAsync(); return(Ok()); } return(NotFound()); } catch (DbUpdateConcurrencyException) { return(StatusCode(500)); } }
public async Task Delete_ReturnsHairLengthLinkDeleted() { // Arrange _hairLengthLinksContext = _db.SeedHairLengthLinksContext(); ulong id = 2; List <HairLengthLinks> currentHairLengthLinks = _db.HairLengthLinks; int currentHairLengthLinksCount = _db.HairLengthLinks.Count; HairLengthLinks expected = _db.HairLengthLinks.FirstOrDefault(u => u.Id == id); // Act HairLengthLinks actual = await _hairLengthLinksContext.Delete(id); int updatedHairLengthLinksCount = _db.HairLengthLinks.Count; _db.HairLengthLinks = new List <HairLengthLinks>(currentHairLengthLinks); // Assert Assert.Equal(expected.Id, actual.Id); Assert.Equal(currentHairLengthLinksCount - 1, updatedHairLengthLinksCount); }
public async Task <IActionResult> PutHairLengthLinks(ulong id, [FromBody] HairLengthLinks hairLengthLinks) { if (!_authorizationService.ValidateJWTCookie(Request)) { return(Unauthorized(new { errors = new { Token = new string[] { "Invalid token" } }, status = 401 })); } if (id != hairLengthLinks.Id) { return(BadRequest(new { errors = new { Id = new string[] { "ID sent does not match the one in the endpoint" } }, status = 400 })); } var correspondingHairLength = await _context.HairLengths.FirstOrDefaultAsync(h => h.Id == hairLengthLinks.HairLengthId); if (correspondingHairLength == null) { return(NotFound(new { errors = new { HairLengthId = new string[] { "No matching hair length entry was found" } }, status = 404 })); } _context.Entry(hairLengthLinks).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!HairLengthLinksExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }