Ejemplo n.º 1
0
        /// <summary>
        /// Deletes a hairLength by ID
        /// </summary>
        /// <param name="id">ID of the hairLength to be deleted</param>
        /// <exception cref="ResourceNotFoundException">
        ///     Thrown if the user with the corresponding <seealso cref="id"/> is not found
        /// </exception>
        /// <returns>HairLength deleted</returns>
        public async Task <HairLengths> Delete(ulong id)
        {
            HairLengths hairLength = null;

            if (_context != null)
            {
                hairLength = await _context.HairLengths.FindAsync(id);

                if (hairLength != null)
                {
                    _context.HairLengths.Remove(hairLength);
                    await _context.SaveChangesAsync();
                }
                else
                {
                    throw new ResourceNotFoundException("HairLength not found");
                }
            }
            else
            {
                hairLength = HairLengths.Where(u => u.Id == id).FirstOrDefault();

                if (hairLength != null)
                {
                    HairLengths.Remove(hairLength);
                }
                else
                {
                    throw new ResourceNotFoundException("HairLength not found");
                }
            }

            return(hairLength);
        }
        public async Task <IActionResult> PutHairLengths(ulong id, [FromBody] HairLengths hairLengths)
        {
            if (!_authorizationService.ValidateJWTToken(Request))
            {
                return(Unauthorized(new { errors = new { Token = new string[] { "Invalid token" } }, status = 401 }));
            }

            if (id != hairLengths.Id)
            {
                return(BadRequest(new { errors = new { Id = new string[] { "ID sent does not match the one in the endpoint" } }, status = 400 }));
            }

            HairLengths currentHL = await _context.HairLengths.FindAsync(id);

            try
            {
                if (currentHL != null)
                {
                    currentHL.HairLengthName        = hairLengths.HairLengthName;
                    _context.Entry(currentHL).State = EntityState.Modified;
                    await _context.SaveChangesAsync();

                    return(Ok());
                }
                return(NotFound());
            }
            catch (DbUpdateConcurrencyException)
            {
                return(StatusCode(500));
            }
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> PutHairLengths(ulong id, [FromBody] HairLengths hairLengths)
        {
            if (!_authorizationService.ValidateJWTCookie(Request))
            {
                return(Unauthorized(new { errors = new { Token = new string[] { "Invalid token" } }, status = 401 }));
            }

            if (id != hairLengths.Id)
            {
                return(BadRequest(new { errors = new { Id = new string[] { "ID sent does not match the one in the endpoint" } }, status = 400 }));
            }

            _context.Entry(hairLengths).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!HairLengthsExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task Edit_ReturnsTrue()
        {
            // Arrange
            _hairLengthsContext = _db.SeedHairLengthsContext();
            ulong id = 2;
            List <HairLengths> currentHairLengths = _db.HairLengths;
            HairLengths        current            = currentHairLengths.FirstOrDefault(c => c.Id == id);
            HairLengths        updated            = current.ShallowCopy();

            updated.HairLengthName = "long";

            HairLengths updatedHairLength = new HairLengths
            {
                Id             = id,
                HairLengthName = updated.HairLengthName
            };

            bool expected = true;

            // Act
            bool actual = await _hairLengthsContext.Edit(id, updatedHairLength);

            HairLengths u = _db.HairLengths.FirstOrDefault(c => c.Id == id);

            _db.HairLengths = new List <HairLengths>(currentHairLengths);

            // Assert
            Assert.Equal(expected, actual);
            Assert.Equal(updatedHairLength.HairLengthName, u.HairLengthName);
        }
        public async Task <ActionResult <HairLengths> > PostHairLengths([FromBody] HairLengths hairLengths)
        {
            if (!_authorizationService.ValidateJWTToken(Request))
            {
                return(Unauthorized(new { errors = new { Token = new string[] { "Invalid token" } }, status = 401 }));
            }

            _context.HairLengths.Add(hairLengths);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetHairLengths", new { id = hairLengths.Id }, hairLengths));
        }
        public async Task Read_ReturnsHairLengthById()
        {
            // Arrange
            _hairLengthsContext = _db.SeedHairLengthsContext();
            ulong       id       = 1;
            HairLengths expected = _db.HairLengths.FirstOrDefault(c => c.Id == id);

            // Act
            HairLengths actual = await _hairLengthsContext.ReadById(id);

            // Assert
            Assert.Equal(expected, actual);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Adds a new hair length
        /// <para>
        ///     NOTE: Currently this method is not restricted to unique hair lengths, so the operation always succeeds
        /// </para>
        /// </summary>
        /// <param name="hairLength">Hair Length to be added</param>
        /// <returns>Hair Length added</returns>
        public Task <HairLengths> Add(HairLengths hairLength)
        {
            HairLengths hairLengthAdded = null;

            if (_context != null)
            {
                _context.HairLengths.Add(hairLength);
                hairLengthAdded = hairLength;
            }
            else
            {
                HairLengths.Add(hairLength);
                hairLengthAdded = hairLength;
            }

            return(Task.FromResult(hairLengthAdded));
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Updates an existing hairLength
        /// </summary>
        /// <param name="id">ID of the hairLength to be updated</param>
        /// <param name="updatedHairLength">Updated hairLength object</param>
        /// <exception cref="ResourceNotFoundException">
        ///     Thrown if <seealso cref="updatedHairLength"/> does not exist
        /// </exception>
        /// <exception cref="DbUpdateConcurrencyException">
        /// </exception>
        /// <returns>Result of the operation</returns>
        public async Task <bool> Edit(ulong id, HairLengths updatedHairLength)
        {
            bool hairLengthUpdated = false;

            if (_context != null)
            {
                HairLengths currentHairLength = await _context.HairLengths.FindAsync(id);

                try
                {
                    if (currentHairLength != null)
                    {
                        currentHairLength.HairLengthName        = updatedHairLength.HairLengthName;
                        _context.Entry(currentHairLength).State = EntityState.Modified;
                        await _context.SaveChangesAsync();

                        hairLengthUpdated = true;
                    }

                    throw new ResourceNotFoundException("HairLength not found");
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    throw ex;
                }
            }
            else
            {
                HairLengths currentFaceShape = HairLengths.FirstOrDefault(hl => hl.Id == id);

                if (currentFaceShape != null)
                {
                    int currentHairLengthIndex = HairLengths.FindIndex(c => c.Id == id);
                    HairLengths[currentHairLengthIndex] = updatedHairLength;
                    hairLengthUpdated = true;
                }
                else
                {
                    throw new ResourceNotFoundException("HairLength not found");
                }
            }

            return(hairLengthUpdated);
        }
        public async Task Delete_ReturnsHairLengthDeleted()
        {
            // Arrange
            _hairLengthsContext = _db.SeedHairLengthsContext();
            ulong id = 2;
            List <HairLengths> currentHairLengths = _db.HairLengths;
            int         currentHairLengthsCount   = _db.HairLengths.Count;
            HairLengths expected = _db.HairLengths.FirstOrDefault(u => u.Id == id);

            // Act
            HairLengths actual = await _hairLengthsContext.Delete(id);

            int updatedHairLengthsCount = _db.HairLengths.Count;

            _db.HairLengths = new List <HairLengths>(currentHairLengths);

            // Assert
            Assert.Equal(expected.Id, actual.Id);
            Assert.Equal(currentHairLengthsCount - 1, updatedHairLengthsCount);
        }
        public async Task Add_ReturnsHairLengthAdded()
        {
            // Arrange
            _hairLengthsContext = _db.SeedHairLengthsContext();
            int currentHairLengthsCount           = _db.HairLengths.Count;
            List <HairLengths> currentHairLengths = _db.HairLengths;

            HairLengths expected = new HairLengths
            {
                Id             = 3,
                HairLengthName = "long"
            };

            // Act
            HairLengths actual = await _hairLengthsContext.Add(expected);

            int updatedHairLengthsCount = _db.HairLengths.Count;

            _db.HairLengths = new List <HairLengths>(currentHairLengths);

            // Assert
            Assert.Equal(expected.Id, actual.Id);
            Assert.Equal(currentHairLengthsCount + 1, updatedHairLengthsCount);
        }