| 1 | #region license
|
|---|
| 2 | /*
|
|---|
| 3 | Copyright (c) 2008, Kouji Yamaguchi
|
|---|
| 4 | All rights reserved.
|
|---|
| 5 |
|
|---|
| 6 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|---|
| 7 |
|
|---|
| 8 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|---|
| 9 |
|
|---|
| 10 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|---|
| 11 |
|
|---|
| 12 | * Neither the name of coma2n nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|---|
| 13 |
|
|---|
| 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|---|
| 15 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 16 | IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
|---|
| 17 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
|---|
| 18 | OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|---|
| 19 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 20 | */
|
|---|
| 21 | #endregion
|
|---|
| 22 |
|
|---|
| 23 | #region namespaces
|
|---|
| 24 |
|
|---|
| 25 | using System;
|
|---|
| 26 | using System.IO;
|
|---|
| 27 | using System.Web;
|
|---|
| 28 | using System.Web.Mvc;
|
|---|
| 29 | using System.Net;
|
|---|
| 30 | using System.Linq;
|
|---|
| 31 | using System.Text;
|
|---|
| 32 | using System.Xml.Linq;
|
|---|
| 33 | using System.Diagnostics;
|
|---|
| 34 |
|
|---|
| 35 | using Ele = System.Xml.Linq.XElement;
|
|---|
| 36 | using Attr = System.Xml.Linq.XAttribute;
|
|---|
| 37 |
|
|---|
| 38 | #endregion
|
|---|
| 39 |
|
|---|
| 40 | namespace eLearn_Web.Controllers {
|
|---|
| 41 |
|
|---|
| 42 | #region StudyController class
|
|---|
| 43 |
|
|---|
| 44 | /// <summary>
|
|---|
| 45 | /// スタディに関する情報をやり取りするコントローラクラス
|
|---|
| 46 | /// </summary>
|
|---|
| 47 | public sealed class StudyController : Controller {
|
|---|
| 48 |
|
|---|
| 49 | #region const
|
|---|
| 50 |
|
|---|
| 51 | /// <summary>
|
|---|
| 52 | /// インデックスファイル
|
|---|
| 53 | /// </summary>
|
|---|
| 54 | private const string INDEX_FILE = "~/App_Data/Repo/index.xml";
|
|---|
| 55 | /// <summary>
|
|---|
| 56 | /// コンテントファイルのフォーマット
|
|---|
| 57 | /// </summary>
|
|---|
| 58 | private const string CONTENT_FILE_FORMAT = "~/App_Data/Repo/study/{0}.xml";
|
|---|
| 59 |
|
|---|
| 60 | #endregion
|
|---|
| 61 |
|
|---|
| 62 | #region constructors
|
|---|
| 63 |
|
|---|
| 64 | /// <summary>
|
|---|
| 65 | /// 標準的なコンストラクタ
|
|---|
| 66 | /// </summary>
|
|---|
| 67 | public StudyController() {
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | #endregion
|
|---|
| 71 |
|
|---|
| 72 | #region methods
|
|---|
| 73 |
|
|---|
| 74 | /// <summary>
|
|---|
| 75 | /// スタディ情報の一覧を取得します。
|
|---|
| 76 | /// </summary>
|
|---|
| 77 | /// <returns>スタディ情報の一覧</returns>
|
|---|
| 78 | public ActionResult Index() {
|
|---|
| 79 | var path = Request.MapPath(INDEX_FILE);
|
|---|
| 80 |
|
|---|
| 81 | using(var sr = new StreamReader(path, Encoding.UTF8)) {
|
|---|
| 82 | return Content(sr.ReadToEnd(), "text/xml");
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | /// <summary>
|
|---|
| 87 | /// 指定したIdのスタディの情報を取得します。
|
|---|
| 88 | /// </summary>
|
|---|
| 89 | /// <param name="id">Id</param>
|
|---|
| 90 | /// <returns>スタディ情報</returns>
|
|---|
| 91 | public ActionResult Get(int? id) {
|
|---|
| 92 | if(id != null) {
|
|---|
| 93 | try {
|
|---|
| 94 | var path = Request.MapPath(string.Format(CONTENT_FILE_FORMAT, id));
|
|---|
| 95 |
|
|---|
| 96 | using(var sr = new StreamReader(path, Encoding.UTF8)) {
|
|---|
| 97 | return Content(sr.ReadToEnd(), "text/xml");
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | } catch(Exception exp) {
|
|---|
| 101 | throw new HttpException((int)HttpStatusCode.NotFound, "指定したStudyは存在しません。", exp);
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | } else {
|
|---|
| 105 | throw new HttpException((int)HttpStatusCode.BadRequest, "[id]を指定して下さい。");
|
|---|
| 106 | }
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /// <summary>
|
|---|
| 110 | /// スタディを新規作成します。
|
|---|
| 111 | /// </summary>
|
|---|
| 112 | /// <param name="title">タイトル</param>
|
|---|
| 113 | /// <returns>スタディ情報</returns>
|
|---|
| 114 | public ActionResult New(string title) {
|
|---|
| 115 | if(string.IsNullOrEmpty(title)) {
|
|---|
| 116 | throw new HttpException((int)HttpStatusCode.BadRequest, "[title]を指定して下さい。");
|
|---|
| 117 | }
|
|---|
| 118 | var indexXml = OpenIndex();
|
|---|
| 119 | var id = indexXml.Descendants("study").Max(s => (int)s.Attribute("id")) + 1;
|
|---|
| 120 |
|
|---|
| 121 | indexXml.Root.Add(
|
|---|
| 122 | new Ele("study",
|
|---|
| 123 | new Attr("id", id), new Attr("title", title)
|
|---|
| 124 | )
|
|---|
| 125 | );
|
|---|
| 126 | SaveIndex(indexXml);
|
|---|
| 127 |
|
|---|
| 128 | var contentXml = new XDocument(
|
|---|
| 129 | new Ele("study",
|
|---|
| 130 | new Attr("id", id), new Attr("title", title)
|
|---|
| 131 | )
|
|---|
| 132 | );
|
|---|
| 133 | return Content(
|
|---|
| 134 | SaveContent(contentXml, id).ToString(), "text/xml"
|
|---|
| 135 | );
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | /// <summary>
|
|---|
| 139 | /// 指定したIdのスタディを削除します。
|
|---|
| 140 | /// </summary>
|
|---|
| 141 | /// <param name="id">Id</param>
|
|---|
| 142 | /// <returns>削除できたかどうか</returns>
|
|---|
| 143 | public ActionResult Remove(int? id) {
|
|---|
| 144 | if(id == null) {
|
|---|
| 145 | throw new HttpException((int)HttpStatusCode.BadRequest, "[id]を指定して下さい。");
|
|---|
| 146 | }
|
|---|
| 147 | var indexXml = OpenIndex();
|
|---|
| 148 |
|
|---|
| 149 | var study = indexXml.Descendants("study")
|
|---|
| 150 | .FirstOrDefault(s => (int)s.Attribute("id") == id.Value);
|
|---|
| 151 |
|
|---|
| 152 | if(study == null) {
|
|---|
| 153 | throw new HttpException((int)HttpStatusCode.NotFound, "指定したStudyは存在しません。");
|
|---|
| 154 | }
|
|---|
| 155 | study.Remove();
|
|---|
| 156 |
|
|---|
| 157 | SaveIndex(indexXml);
|
|---|
| 158 |
|
|---|
| 159 | return Content("0", "text/plain");
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | /// <summary>
|
|---|
| 163 | /// 指定したIdのスタディに指定した質問を追加します。
|
|---|
| 164 | /// </summary>
|
|---|
| 165 | /// <param name="id">Id</param>
|
|---|
| 166 | /// <param name="content">質問内容を表すXML</param>
|
|---|
| 167 | /// <returns>スタディ情報</returns>
|
|---|
| 168 | public ActionResult AddQuestion(int? id, string content) {
|
|---|
| 169 | if(id == null || string.IsNullOrEmpty(content)) {
|
|---|
| 170 | throw new HttpException((int)HttpStatusCode.BadRequest, "[id][content]を指定して下さい。");
|
|---|
| 171 | }
|
|---|
| 172 | var xml = OpenContent(id);
|
|---|
| 173 | xml.Root.Add(content);
|
|---|
| 174 |
|
|---|
| 175 | return Content(
|
|---|
| 176 | SaveContent(xml, id).ToString(), "text/xml"
|
|---|
| 177 | );
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | /// <summary>
|
|---|
| 181 | /// 指定したIdのスタディの指定した位置の質問を削除します。
|
|---|
| 182 | /// </summary>
|
|---|
| 183 | /// <param name="id">Id</param>
|
|---|
| 184 | /// <param name="index">質問の位置</param>
|
|---|
| 185 | /// <returns>削除後のスタディ情報</returns>
|
|---|
| 186 | public ActionResult RemoveQuestion(int? id, int? index) {
|
|---|
| 187 | if(id == null || index == null) {
|
|---|
| 188 | throw new HttpException((int)HttpStatusCode.BadRequest, "[id][index]を指定して下さい。");
|
|---|
| 189 | }
|
|---|
| 190 | var contentXml = OpenContent(id);
|
|---|
| 191 | // 一度リストに変換しないと駄目よ!!
|
|---|
| 192 | contentXml.Descendants("question")
|
|---|
| 193 | .Where((q, i) => i == index)
|
|---|
| 194 | .ToList().ForEach(q => q.Remove());
|
|---|
| 195 |
|
|---|
| 196 | return Content(
|
|---|
| 197 | SaveContent(contentXml, id).ToString(), "text/xml"
|
|---|
| 198 | );
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | #region private
|
|---|
| 202 |
|
|---|
| 203 | /// <summary>
|
|---|
| 204 | /// インデックスファイルを開きます。
|
|---|
| 205 | /// </summary>
|
|---|
| 206 | /// <returns>インデックスファイル</returns>
|
|---|
| 207 | XDocument OpenIndex() {
|
|---|
| 208 | return XDocument.Load(Request.MapPath(INDEX_FILE));
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | /// <summary>
|
|---|
| 212 | /// 指定したインデックスを保存します。
|
|---|
| 213 | /// </summary>
|
|---|
| 214 | /// <param name="xml">インデックス</param>
|
|---|
| 215 | /// <returns>インデックス</returns>
|
|---|
| 216 | XDocument SaveIndex(XDocument xml) {
|
|---|
| 217 | xml.Save(Request.MapPath(INDEX_FILE));
|
|---|
| 218 |
|
|---|
| 219 | return xml;
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | /// <summary>
|
|---|
| 223 | /// 指定したIdのコンテントを開きます。
|
|---|
| 224 | /// </summary>
|
|---|
| 225 | /// <param name="id">Id</param>
|
|---|
| 226 | /// <returns>コンテント</returns>
|
|---|
| 227 | XDocument OpenContent(int? id) {
|
|---|
| 228 | return XDocument.Load(
|
|---|
| 229 | Request.MapPath(string.Format(CONTENT_FILE_FORMAT, id))
|
|---|
| 230 | );
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | /// <summary>
|
|---|
| 234 | /// 指定したコンテントを保存します。
|
|---|
| 235 | /// </summary>
|
|---|
| 236 | /// <param name="xml">コンテント</param>
|
|---|
| 237 | /// <param name="id">Id</param>
|
|---|
| 238 | /// <returns>コンテント</returns>
|
|---|
| 239 | XDocument SaveContent(XDocument xml, int? id) {
|
|---|
| 240 | xml.Save(
|
|---|
| 241 | Request.MapPath(string.Format(CONTENT_FILE_FORMAT, id))
|
|---|
| 242 | );
|
|---|
| 243 | return xml;
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | #endregion
|
|---|
| 247 |
|
|---|
| 248 | #endregion
|
|---|
| 249 |
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | #endregion
|
|---|
| 253 |
|
|---|
| 254 | }
|
|---|