public ActionResult GetByComment(Guid commentId, [FromBody] NullableGuidDeserializer nullableUserId = null)
        {
            Guid?userId = nullableUserId.Property;

            Comment record;

            try
            {
                record = db.FindRecordById <Comment>(COMMENT_TABLE, commentId);
            }
            catch (Exception e)
            {
                Failure failure = FailureFact.Default(e, userId);
                db.InsertRecord(FAILURE_TABLE, failure);
                return(StatusCode(500, failure));
            }

            if (record == null)
            {
                Failure failure = FailureFact.IdNotFound(null, userId, commentId);
                db.InsertRecord(FAILURE_TABLE, failure);
                return(NotFound(failure));
            }

            Success success = SuccessFact.CommentRetrieved(commentId, userId);

            db.InsertRecord(SUCCESS_TABLE, success);
            return(new OkObjectResult(record));
        }
        public IActionResult Get(Guid reviewId, [FromBody] NullableGuidDeserializer nullableUserId = null)
        {
            Guid?userId = nullableUserId.Property;

            Review record;

            try
            {
                record = db.FindRecordById <Review>(REVIEW_TABLE, reviewId);
            }
            catch (Exception e)
            {
                Failure failure = FailureFact.Default(e, userId);
                db.InsertRecordAsync(FAILURE_TABLE, failure);
                return(StatusCode(500, failure));
            }

            if (record == null)
            {
                Failure failure = FailureFact.IdNotFound(null, userId, reviewId);
                db.InsertRecordAsync(FAILURE_TABLE, failure);
                return(NotFound(failure));
            }

            Success success = SuccessFact.ReviewRetrieved(reviewId, userId);

            db.InsertRecordAsync(SUCCESS_TABLE, success);

            return(new OkObjectResult(record));
        }
        public ActionResult GetByReview(Guid reviewId, [FromBody] NullableGuidDeserializer nullableUserId = null)
        {
            Guid?userId = nullableUserId.Property;

            List <Comment> records;
            List <Filter>  filters = ParseQuery.ParseFilters("ReivewId", reviewId.ToString());

            try
            {
                records = db.LoadRecords <Comment>(COMMENT_TABLE, filters, null, null);
            }
            catch (Exception e)
            {
                Failure failure = FailureFact.Default(e, userId);
                db.InsertRecord(FAILURE_TABLE, failure);
                return(StatusCode(500, failure));
            }

            if (records == null)
            {
                Failure failure = FailureFact.NoRecordsFound(null, userId, null, filters, null);
                db.InsertRecord(FAILURE_TABLE, failure);
                return(NotFound(failure));
            }

            Success success = SuccessFact.CommentsByReviewRetrieved(reviewId, userId);

            db.InsertRecord(SUCCESS_TABLE, success);
            return(new OkObjectResult(records));
        }
        public IActionResult Patch(Guid reviewId, [FromQuery] Guid userId)
        {
            Review record;

            try
            {
                record = db.FindDeletedRecordById <Review>(REVIEW_TABLE, reviewId);
            }
            catch (Exception e)
            {
                Failure failure = FailureFact.Default(e, userId);
                db.InsertRecordAsync(FAILURE_TABLE, failure);
                return(StatusCode(500, failure));
            }

            if (record == null)
            {
                Failure failure = FailureFact.IdNotFound(null, userId, reviewId);
                db.InsertRecordAsync(FAILURE_TABLE, failure);
                return(NotFound(failure));
            }
            else
            {
                record.ReInstate(Guid.Empty);
                db.PutRecord(REVIEW_TABLE, record, reviewId);
            }

            Success success = SuccessFact.ReviewReinstated(reviewId, userId);

            db.InsertRecordAsync(SUCCESS_TABLE, success);

            return(NoContent());
        }
        public IActionResult Delete(Guid reviewId, [FromQuery] Guid userId)
        {
            Review record;

            try
            {
                record = db.FindRecordById <Review>(REVIEW_TABLE, reviewId);
            }
            catch (Exception e)
            {
                Failure failure = FailureFact.BadUserId(e, null);
                db.InsertRecordAsync(FAILURE_TABLE, failure);
                return(BadRequest(failure));
            }

            if (record == null)
            {
                Failure failure = FailureFact.IdNotFound(null, userId, reviewId);
                db.InsertRecordAsync(FAILURE_TABLE, failure);
                return(NotFound(failure));
            }
            else
            {
                record.Delete(Guid.Empty);
                db.PutRecord(REVIEW_TABLE, record, reviewId);
            }

            Success success = SuccessFact.ReviewSoftDelete(reviewId, userId);

            db.InsertRecordAsync(SUCCESS_TABLE, success);

            return(NoContent());
        }
        public IActionResult Get([FromQuery(Name = "sortDirection")] string sortDirection = "asc", [FromQuery(Name = "sortField")] string sortField = "Time", [FromQuery(Name = "filterFields")] string filterFields = null, [FromQuery(Name = "filterValues")] string filterValues = null, [FromQuery(Name = "pageNumber")] int pageNumber = 0, [FromQuery(Name = "pageItems")] int pageItems = 10, [FromBody] NullableGuidDeserializer nullableUserId = null)
        {
            Guid?userId = nullableUserId.Property;

            Sort sort = ParseQuery.ParseSort(sortDirection, sortField);
            Page page = ParseQuery.ParsePage(pageNumber, pageItems);

            List <Filter> filters;

            try
            {
                filters = ParseQuery.ParseFilters(filterFields, filterValues);
            }
            catch (Exception e)
            {
                Failure failure = FailureFact.UnevenFilters(e, userId, filterValues, filterFields);
                db.InsertRecordAsync(FAILURE_TABLE, failure);
                return(BadRequest(failure));
            }

            List <Review> records;

            try
            {
                records = db.LoadRecords <Review>(REVIEW_TABLE, filters, sort, page);
            }
            catch (Exception e)
            {
                Failure failure = FailureFact.Default(e, userId);
                db.InsertRecordAsync(FAILURE_TABLE, failure);
                return(StatusCode(500, failure));
            }

            if (records.Count == 0)
            {
                Failure failure = FailureFact.NoRecordsFound(null, userId, sort, filters, page);
                db.InsertRecordAsync(FAILURE_TABLE, failure);
                return(NotFound(failure));
            }

            List <PartialReview> partialRecords = new List <PartialReview>();

            foreach (Review record in records)
            {
                partialRecords.Add(record.ToPartialReview());
            }

            Success success = SuccessFact.ReviewsRetrieved(userId, sort, filters, page);

            db.InsertRecordAsync(SUCCESS_TABLE, success);

            return(new OkObjectResult(partialRecords));
        }
        public IActionResult Post([FromBody] Review review)
        {
            try
            {
                db.InsertRecord(REVIEW_TABLE, review);
            }
            catch (Exception e)
            {
                Failure failure = FailureFact.Default(e, review.UserId);
                db.InsertRecordAsync(FAILURE_TABLE, failure);
                return(StatusCode(500, failure));
            }

            Success success = SuccessFact.ReviewCreated(review.ReviewId, review.UserId);

            db.InsertRecordAsync(SUCCESS_TABLE, success);
            return(Created($"https://www.ReviewThis.dev/{GuidToBase64.EncodeBase64String(review.ReviewId)}", success));
        }
        public IActionResult Put(Guid reviewId, [FromBody] JObject value)
        {
            Guid userId;

            try
            {
                userId = value["userId"].ToObject <Guid>();
            }
            catch (Exception e)
            {
                Failure failure = FailureFact.BadUserId(e, value["userId"]);
                db.InsertRecordAsync(FAILURE_TABLE, failure);
                return(BadRequest(failure));
            }

            List <Section> sections = null;
            List <Guid>    comments = null;

            try
            {
                if (value["sections"] != null)
                {
                    sections = value["sections"].ToObject <List <Section> >();
                }
                else
                {
                    comments = value["comments"].ToObject <List <Guid> >();
                }
            }
            catch (Exception e)
            {
                Failure failure = FailureFact.BadRequestBody(e, userId, value["sections"]);
                db.InsertRecordAsync(FAILURE_TABLE, failure);
                return(BadRequest(failure));
            }

            Review record;

            try
            {
                record = db.FindRecordById <Review>(REVIEW_TABLE, reviewId);
            }
            catch (Exception e)
            {
                Failure failure = FailureFact.Default(e, userId);
                db.InsertRecordAsync(FAILURE_TABLE, failure);
                return(StatusCode(500, failure));
            }

            if (record == null)
            {
                Failure failure = FailureFact.IdNotFound(null, userId, reviewId);
                db.InsertRecordAsync(FAILURE_TABLE, failure);
                return(NotFound(failure));
            }

            if (sections != null)
            {
                record.SetSection(sections, userId);
            }
            else
            {
                record.SetComments(comments, userId);
            }

            db.PutRecord(REVIEW_TABLE, value, reviewId);

            Success success = SuccessFact.ReviewSectionsModified(reviewId, userId, sections);

            db.InsertRecordAsync(SUCCESS_TABLE, success);

            return(NoContent());
        }