Example #1
0
        public async Task <IActionResult> Delete([FromRoute] int id)
        {
            var person = await _context.Persons.FindAsync(id);

            if (person == null)
            {
                return(NotFound());
            }

            try
            {
                var deletedPerson = _context.Persons.Remove(person);

                if (deletedPerson.Entity == null)
                {
                    return(BadRequest("Error while deleting."));
                }

                ICloudBlobService cloudBlobService = new CloudBlobService();
                await cloudBlobService.DeleteFaceBlobAsync(id.ToString() + "-container");

                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                return(BadRequest("Error: " + ex.Message + Environment.NewLine + Environment.NewLine + ex.StackTrace + Environment.NewLine + Environment.NewLine + ex.Source));
            }
            return(Ok(true));
        }
Example #2
0
        public async Task <IActionResult> Post([FromBody] Person person)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(string.Join(Environment.NewLine, ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage))));
            }

            var created = _context.Persons.Add(person);

            if (created.Entity == null)
            {
                return(BadRequest("Error while adding user to database"));
            }

            try
            {
                _context.SaveChanges();

                ICloudBlobService  cloudBlobService = new CloudBlobService();
                ICollection <Face> faceBlobs        = await cloudBlobService.UploadFaceBlobAsync(created.Entity.Id, created.Entity.Id.ToString() + _containerNamePrefix, person.Photos);

                foreach (Face blob in faceBlobs)
                {
                    var faceBlobCreated = _context.Faces.Add(blob);

                    if (faceBlobCreated.Entity == null)
                    {
                        return(BadRequest("Error while adding face to database"));
                    }
                }
                _context.SaveChanges();
            }
            catch (Exception ex)
            {
                return(BadRequest("Error: " + ex.Message + Environment.NewLine + Environment.NewLine + ex.StackTrace + Environment.NewLine + Environment.NewLine + ex.Source));
            }

            return(Ok(created.Entity));
        }
Example #3
0
        public async Task <IActionResult> Put([FromBody] Person person, [FromRoute] int id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                var updated = _context.Persons.Update(person);

                if (updated.Entity == null)
                {
                    return(BadRequest("Error while updating."));
                }

                ICloudBlobService  cloudBlobService = new CloudBlobService();
                ICollection <Face> faceBlobs        = await cloudBlobService.UploadFaceBlobAsync(id, id.ToString() + _containerNamePrefix, person.Photos);

                foreach (Face blob in faceBlobs)
                {
                    var faceBlobCreated = _context.Faces.Add(blob);

                    if (faceBlobCreated.Entity == null)
                    {
                        return(BadRequest("Error while adding face to database"));
                    }
                }
                _context.SaveChanges();
            }
            catch (Exception ex)
            {
                return(BadRequest("Error: " + ex.Message + Environment.NewLine + Environment.NewLine + ex.StackTrace + Environment.NewLine + Environment.NewLine + ex.Source));
            }

            return(Ok());
        }