<commit_before>﻿using System.Net;
using System.Threading.Tasks;
using System.Web;
using LtiLibrary.AspNet.Outcomes.v2;
using LtiLibrary.Core.Outcomes.v2;

namespace SimpleLti.Controllers
{
    public class ResultsController : ResultsControllerBase
    {
        // Simple "database" of results for demonstration purposes
        public const string ResultId = "result-1";
        private static LisResult _result;

        public ResultsController()
        {
            OnDeleteResult = context =>
            {
                var resultUri = RoutingHelper.GetResultsUri(new HttpContextWrapper(HttpContext.Current), context.ContextId, context.LineItemId, context.Id);

                if (resultUri == null || _result == null)
                {
                    context.StatusCode = HttpStatusCode.NotFound;
                }
                else
                {
                    _result = null;
                    context.StatusCode = HttpStatusCode.OK;
                }
                return Task.FromResult<object>(null);
            };

            OnGetResult = context =>
            {
                var resultUri = RoutingHelper.GetResultsUri(new HttpContextWrapper(HttpContext.Current), context.ContextId, context.LineItemId, context.Id);

                if (resultUri == null || _result == null)
                {
                    context.StatusCode = HttpStatusCode.NotFound;
                }
                else
                {
                    context.Result = _result;
                    context.StatusCode = HttpStatusCode.OK;
                }
                return Task.FromResult<object>(null);
            };

            OnGetResults = context =>
            {
                if (_result == null)
                {
                    context.StatusCode = HttpStatusCode.NotFound;
                }
                else
                {
                    context.ResultContainerPage = new ResultContainerPage
                    {
                        Id = RoutingHelper.GetResultsUri(new HttpContextWrapper(HttpContext.Current), context.ContextId, context.LineItemId),
                        ResultContainer = new ResultContainer
                        {
                            MembershipSubject = new ResultMembershipSubject
                            {
                                Results = new[] { _result }
                            }
                        }
                    };
                    context.StatusCode = HttpStatusCode.OK;
                }
                return Task.FromResult<object>(null);
            };

            OnPutResult = context =>
            {
                if (context.Result == null || _result == null || !_result.Id.Equals(context.Result.Id))
                {
                    context.StatusCode = HttpStatusCode.NotFound;
                }
                else
                {
                    _result = context.Result;
                    context.StatusCode = HttpStatusCode.OK;
                }
                return Task.FromResult<object>(null);
            };
        }
    }
}<commit_msg>Move fake db to LineItemsController. Add support for all Result verbs.<commit_after>﻿using System.Net;
using System.Threading.Tasks;
using System.Web;
using LtiLibrary.AspNet.Outcomes.v2;
using LtiLibrary.Core.Common;
using LtiLibrary.Core.Outcomes.v2;

namespace SimpleLti.Controllers
{
    public class ResultsController : ResultsControllerBase
    {
        public ResultsController()
        {
            OnDeleteResult = context =>
            {
                var resultUri = RoutingHelper.GetResultsUri(new HttpContextWrapper(HttpContext.Current), context.ContextId, context.LineItemId, context.Id);

                if (resultUri == null || LineItemsController.Result == null)
                {
                    context.StatusCode = HttpStatusCode.NotFound;
                }
                else
                {
                    LineItemsController.Result = null;
                    context.StatusCode = HttpStatusCode.OK;
                }
                return Task.FromResult<object>(null);
            };

            OnGetResult = context =>
            {
                var lineItemUri = RoutingHelper.GetLineItemsUri(new HttpContextWrapper(HttpContext.Current), context.ContextId, context.LineItemId);
                var resultUri = RoutingHelper.GetResultsUri(new HttpContextWrapper(HttpContext.Current), context.ContextId, context.LineItemId, context.Id);

                if (LineItemsController.LineItem == null || !LineItemsController.LineItem.Id.Equals(lineItemUri))
                {
                    context.StatusCode = HttpStatusCode.NotFound;
                }
                else if (LineItemsController.Result == null || !LineItemsController.Result.Id.Equals(resultUri))
                {
                    // https://www.imsglobal.org/specs/ltiomv2p0/specification-3
                    // When a line item is created, a result for each user is deemed to be created with a status value of “Initialized”.  
                    // Thus, there is no need to actually create a result with a POST request; the first connection to a result may be a PUT or a GET request.
                    context.Result = new LisResult
                    {
                        ExternalContextId = LtiConstants.ResultContextId,
                        Id = resultUri,
                        ResultOf = lineItemUri,
                        ResultStatus = ResultStatus.Initialized
                    };
                    context.StatusCode = HttpStatusCode.OK;
                }
                else
                {
                    context.Result = LineItemsController.Result;
                    context.StatusCode = HttpStatusCode.OK;
                }
                return Task.FromResult<object>(null);
            };

            OnGetResults = context =>
            {
                var lineItemUri = RoutingHelper.GetLineItemsUri(new HttpContextWrapper(HttpContext.Current),
                    context.ContextId, context.LineItemId);
                if (LineItemsController.LineItem == null || !LineItemsController.LineItem.Id.Equals(lineItemUri))
                {
                    context.StatusCode = HttpStatusCode.NotFound;
                }
                else
                {
                    context.ResultContainerPage = new ResultContainerPage
                    {
                        ExternalContextId = LtiConstants.ResultContainerContextId,
                        Id = RoutingHelper.GetResultsUri(new HttpContextWrapper(HttpContext.Current), context.ContextId, context.LineItemId),
                        ResultContainer = new ResultContainer
                        {
                            MembershipSubject = new ResultMembershipSubject
                            {
                                Results = LineItemsController.Result == null ? new LisResult[] {} : new[] { LineItemsController.Result }
                            }
                        }
                    };
                    context.StatusCode = HttpStatusCode.OK;
                }
                return Task.FromResult<object>(null);
            };

            OnPostResult = context =>
            {
                var lineItemUri = RoutingHelper.GetLineItemsUri(new HttpContextWrapper(HttpContext.Current),
                    context.ContextId, context.LineItemId);
                if (LineItemsController.LineItem == null || !LineItemsController.LineItem.Id.Equals(lineItemUri))
                {
                    context.StatusCode = HttpStatusCode.BadRequest;
                }
                else
                {
                    LineItemsController.Result = context.Result;
                    LineItemsController.Result.Id = RoutingHelper.GetResultsUri(new HttpContextWrapper(HttpContext.Current),
                        context.ContextId, context.LineItemId, LineItemsController.ResultId);
                    context.Result = LineItemsController.Result;
                    context.StatusCode = HttpStatusCode.Created;
                }
                return Task.FromResult<object>(null);
            };

            OnPutResult = context =>
            {
                // https://www.imsglobal.org/specs/ltiomv2p0/specification-3
                // When a line item is created, a result for each user is deemed to be created with a status value of “Initialized”.  
                // Thus, there is no need to actually create a result with a POST request; the first connection to a result may be a 
                // PUT or a GET request.

                var lineItemUri = RoutingHelper.GetLineItemsUri(new HttpContextWrapper(HttpContext.Current),
                    context.ContextId, context.LineItemId);
                if (LineItemsController.LineItem == null || !LineItemsController.LineItem.Id.Equals(lineItemUri))
                {
                    context.StatusCode = HttpStatusCode.NotFound;
                }
                else if (!LineItemsController.LineItem.Id.Equals(context.Result.ResultOf))
                {
                    context.StatusCode = HttpStatusCode.BadRequest;
                }
                else
                {
                    // If this is the first connection, the PUT is equivalent to a POST
                    LineItemsController.Result = context.Result;
                    if (context.Result.Id == null)
                    {
                        LineItemsController.Result.Id =
                            RoutingHelper.GetResultsUri(new HttpContextWrapper(HttpContext.Current),
                                context.ContextId, context.LineItemId, LineItemsController.ResultId);
                    }
                    context.StatusCode = HttpStatusCode.OK;
                }
                return Task.FromResult<object>(null);
            };
        }
    }
}
{'fraction_non_alphanumeric': 0.07292817679558011, 'fraction_numerical': 0.0011049723756906078, 'mean_word_length': 1.3592417061611375, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 21, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7647861', 'n_tokens_mistral': 2261, 'n_tokens_neox': 2140, 'n_words': 446}