예제 #1
0
        public async Task <ActionResult <ProblemMetadata> > Import([FromBody] ProblemPackage data)
        {
            IProblemProvider res = await _workspace.Problems.Create(data.Metadata);

            await res.SetDescription(data.Description);

            if (data.Samples != null)
            {
                foreach (ProblemPackage.TestCasePackage v in data.Samples)
                {
                    ITestCaseProvider item = await res.Samples.Create(v.Metadata);
                    await UpdateIOData(new TestCaseData
                    {
                        Input  = v.Input,
                        Output = v.Output
                    }, item);
                }
            }

            if (data.Tests != null)
            {
                foreach (ProblemPackage.TestCasePackage v in data.Tests)
                {
                    ITestCaseProvider item = await res.Tests.Create(v.Metadata);
                    await UpdateIOData(new TestCaseData
                    {
                        Input  = v.Input,
                        Output = v.Output
                    }, item);
                }
            }

            return(Created($"problems/{res.Id}", await res.GetMetadata()));
        }
예제 #2
0
        public async Task <IActionResult> OnPostImportAsync()
        {
            if (!await CheckModel(true))
            {
                return(BadRequest());
            }

            HttpClient     httpclient = clientFactory.CreateClient();
            ProblemsClient client     = new ProblemsClient(httpclient);

            try
            {
                string sp = "";
                using (Stream st = PostData.ImportFile.OpenReadStream())
                    using (StreamReader sr = new StreamReader(st))
                        sp = await sr.ReadToEndAsync();
                ProblemPackage package = Newtonsoft.Json.JsonConvert.DeserializeObject <ProblemPackage>(sp);
                package.Metadata.UserId = PostData.Metadata.UserId;
                ProblemMetadata pro = await client.ImportAsync(package);

                return(RedirectToPage(new { id = pro.Id }));
            }
            catch
            {
                return(NotFound());
            }
        }
예제 #3
0
        public async Task <ActionResult <ProblemPackage> > Export(string id)
        {
            IProblemProvider res = await _workspace.Problems.Get(id);

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

            try
            {
                ProblemPackage package = new ProblemPackage
                {
                    Metadata    = await res.GetMetadata(),
                    Description = await res.GetDescription(),
                };

                {
                    List <ProblemPackage.TestCasePackage> ls = new List <ProblemPackage.TestCasePackage>();
                    IEnumerable <ITestCaseProvider>       ss = await res.Samples.GetAll();

                    foreach (ITestCaseProvider s in ss)
                    {
                        ProblemPackage.TestCasePackage t = new ProblemPackage.TestCasePackage
                        {
                            Metadata = await s.GetMetadata(),
                            Input    = await TextIO.ToString(await s.GetInput()),
                            Output   = await TextIO.ToString(await s.GetOutput()),
                        };
                        ls.Add(t);
                    }
                    package.Samples = ls;
                }

                {
                    List <ProblemPackage.TestCasePackage> ls = new List <ProblemPackage.TestCasePackage>();
                    IEnumerable <ITestCaseProvider>       ss = await res.Tests.GetAll();

                    foreach (ITestCaseProvider s in ss)
                    {
                        ProblemPackage.TestCasePackage t = new ProblemPackage.TestCasePackage
                        {
                            Metadata = await s.GetMetadata(),
                            Input    = await TextIO.ToString(await s.GetInput()),
                            Output   = await TextIO.ToString(await s.GetOutput()),
                        };
                        ls.Add(t);
                    }
                    package.Tests = ls;
                }

                return(Ok(package));
            }
            catch
            {
                return(NoContent());
            }
        }