public async Task <IActionResult> PutFaceShapes(ulong id, [FromBody] FaceShapes faceShapes)
        {
            if (!_authorizationService.ValidateJWTCookie(Request))
            {
                return(Unauthorized(new { errors = new { Token = new string[] { "Invalid token" } }, status = 401 }));
            }

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

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

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

            return(NoContent());
        }
Beispiel #2
0
        public async Task Edit_ReturnsTrue()
        {
            // Arrange
            _faceShapesContext = _db.SeedFaceShapesContext();
            ulong             id = 2;
            List <FaceShapes> currentFaceShapes = _db.FaceShapes;
            FaceShapes        current           = currentFaceShapes.FirstOrDefault(c => c.Id == id);
            FaceShapes        updated           = current.ShallowCopy();

            updated.ShapeName = "square";

            FaceShapes updatedFaceShape = new FaceShapes
            {
                Id        = id,
                ShapeName = updated.ShapeName
            };

            bool expected = true;

            // Act
            bool actual = await _faceShapesContext.Edit(id, updatedFaceShape);

            FaceShapes u = _db.FaceShapes.FirstOrDefault(fs => fs.Id == id);

            _db.FaceShapes = new List <FaceShapes>(currentFaceShapes);

            // Assert
            Assert.Equal(expected, actual);
            Assert.Equal(updatedFaceShape.ShapeName, updatedFaceShape.ShapeName);
        }
        /// <summary>
        /// Deletes a face shape by ID
        /// </summary>
        /// <param name="id">ID of the face shape to be deleted</param>
        /// <exception cref="ResourceNotFoundException">
        ///     Thrown if the face shape with the corresponding <seealso cref="id"/> is not found
        /// </exception>
        /// <returns>Face shape deleted</returns>
        public async Task <FaceShapes> Delete(ulong id)
        {
            FaceShapes faceShape = null;

            if (_context != null)
            {
                faceShape = await _context.FaceShapes.FindAsync(id);

                if (faceShape != null)
                {
                    _context.FaceShapes.Remove(faceShape);
                    await _context.SaveChangesAsync();
                }
                else
                {
                    throw new ResourceNotFoundException("Face shape not found");
                }
            }
            else
            {
                faceShape = FaceShapes.Where(u => u.Id == id).FirstOrDefault();

                if (faceShape != null)
                {
                    FaceShapes.Remove(faceShape);
                }
                else
                {
                    throw new ResourceNotFoundException("Face shape not found");
                }
            }

            return(faceShape);
        }
Beispiel #4
0
        public async Task <IActionResult> PutFaceShapes(ulong id, [FromBody] FaceShapes faceShapes)
        {
            if (!_authorizationService.ValidateJWTToken(Request))
            {
                return(Unauthorized(new { errors = new { Token = new string[] { "Invalid token" } }, status = 401 }));
            }

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

            FaceShapes currentFaceShape = await _context.FaceShapes.FindAsync(id);

            try
            {
                if (currentFaceShape != null)
                {
                    currentFaceShape.ShapeName             = faceShapes.ShapeName;
                    _context.Entry(currentFaceShape).State = EntityState.Modified;
                    await _context.SaveChangesAsync();

                    return(Ok());
                }
                return(NotFound());
            }
            catch (DbUpdateConcurrencyException)
            {
                return(StatusCode(500));
            }
        }
Beispiel #5
0
        public async Task <ActionResult <FaceShapes> > PostFaceShapes([FromBody] FaceShapes faceShapes)
        {
            if (!_authorizationService.ValidateJWTToken(Request))
            {
                return(Unauthorized(new { errors = new { Token = new string[] { "Invalid token" } }, status = 401 }));
            }

            _context.FaceShapes.Add(faceShapes);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetFaceShapes", new { id = faceShapes.Id }, faceShapes));
        }
Beispiel #6
0
        public async Task Read_ReturnsFaceShapeById()
        {
            // Arrange
            _faceShapesContext = _db.SeedFaceShapesContext();
            ulong      id       = 1;
            FaceShapes expected = _db.FaceShapes.FirstOrDefault(c => c.Id == id);

            // Act
            FaceShapes actual = await _faceShapesContext.ReadById(id);

            // Assert
            Assert.Equal(expected, actual);
        }
        /// <summary>
        /// Adds a new face shape
        /// <para>
        ///     NOTE: Currently this method is not restricted to unique face shapes, so the operation always succeeds
        /// </para>
        /// </summary>
        /// <param name="faceShape">Face shape to be added</param>
        /// <returns>Face shape added</returns>
        public Task <FaceShapes> Add(FaceShapes faceShape)
        {
            FaceShapes faceShapeAdded = null;

            if (_context != null)
            {
                _context.FaceShapes.Add(faceShape);
                faceShapeAdded = faceShape;
            }
            else
            {
                FaceShapes.Add(faceShape);
                faceShapeAdded = faceShape;
            }

            return(Task.FromResult(faceShapeAdded));
        }
        /// <summary>
        /// Updates an existing face shape
        /// </summary>
        /// <param name="id">ID of the face shape to be updated</param>
        /// <param name="faceShape">Updated face shape object</param>
        /// <exception cref="ResourceNotFoundException">
        ///     Thrown if <seealso cref="faceShape"/> does not exist
        /// </exception>
        /// <exception cref="DbUpdateConcurrencyException">
        /// </exception>
        /// <returns>Result of the operation</returns>
        public async Task <bool> Edit(ulong id, FaceShapes faceShape)
        {
            bool faceShapeUpdated = false;

            if (_context != null)
            {
                FaceShapes currentFaceShape = await _context.FaceShapes.FindAsync(id);

                try
                {
                    if (currentFaceShape != null)
                    {
                        currentFaceShape.ShapeName             = faceShape.ShapeName;
                        _context.Entry(currentFaceShape).State = EntityState.Modified;
                        await _context.SaveChangesAsync();

                        faceShapeUpdated = true;
                    }
                    {
                        throw new ResourceNotFoundException("Face shape not found");
                    }
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    throw ex;
                }
            }
            else
            {
                FaceShapes currentFaceShape = FaceShapes.FirstOrDefault(fs => fs.Id == id);

                if (currentFaceShape != null)
                {
                    int currentFSIndex = FaceShapes.FindIndex(fs => fs.Id == id);
                    FaceShapes[currentFSIndex] = faceShape;
                    faceShapeUpdated           = true;
                }
                else
                {
                    throw new ResourceNotFoundException("Face shape not found");
                }
            }

            return(faceShapeUpdated);
        }
Beispiel #9
0
        public async Task Delete_ReturnsFaceShapeDeleted()
        {
            // Arrange
            _faceShapesContext = _db.SeedFaceShapesContext();
            ulong             id = 2;
            List <FaceShapes> currentFaceShapes = _db.FaceShapes;
            int        currentFaceShapesCount   = _db.FaceShapes.Count;
            FaceShapes expected = _db.FaceShapes.FirstOrDefault(u => u.Id == id);

            // Act
            FaceShapes actual = await _faceShapesContext.Delete(id);

            int updatedFaceShapesCount = _db.FaceShapes.Count;

            _db.FaceShapes = new List <FaceShapes>(currentFaceShapes);

            // Assert
            Assert.Equal(expected.Id, actual.Id);
            Assert.Equal(currentFaceShapesCount - 1, updatedFaceShapesCount);
        }
Beispiel #10
0
        public async Task Add_ReturnsFaceShapeAdded()
        {
            // Arrange
            _faceShapesContext = _db.SeedFaceShapesContext();
            int currentFaceShapesCount          = _db.FaceShapes.Count;
            List <FaceShapes> currentFaceShapes = _db.FaceShapes;

            FaceShapes expected = new FaceShapes
            {
                Id        = 3,
                ShapeName = "round"
            };

            // Act
            FaceShapes actual = await _faceShapesContext.Add(expected);

            int updatedFaceShapesCount = _db.FaceShapes.Count;

            _db.FaceShapes = new List <FaceShapes>(currentFaceShapes);

            // Assert
            Assert.Equal(expected.Id, actual.Id);
            Assert.Equal(currentFaceShapesCount + 1, updatedFaceShapesCount);
        }