Ejemplo n.º 1
0
        public async Task <ActionResult <CourseModel> > PostCourseCreate(int areaID, [FromBody] CourseModel course)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                var newCourse = await courseService.AddCourseAsync(areaID, course);

                ResponseIdModel rsp = new ResponseIdModel();
                rsp.id = newCourse.Id.GetValueOrDefault();
                return(Created($"/api/Area/{areaID}/Course/{newCourse.Id}", rsp));
            }
            catch (InvalidOperationException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (NotFoundException ex)
            {
                return(NotFound(ex.Message));
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <LessonModel> > PostLessonCreated(int areaId, int courseId, [FromBody] LessonModel lesson)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            try
            {
                var newLesson = await lessonService.AddLessonAsync(courseId, lesson);

                ResponseIdModel rsp = new ResponseIdModel();
                rsp.id = newLesson.Id.GetValueOrDefault();
                //return Created($"/api/area/courses/sections/{courseId}/lessons/{lesson.Id}", newLesson);
                return(Created($"api/Area/{areaId:int}/Course/{courseId:int}/Lesson/{newLesson.Id}", rsp));
            }
            catch (InvalidOperationException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (NotFoundException ex)
            {
                return(NotFound(ex.Message));
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }
Ejemplo n.º 3
0
        public async Task <ActionResult <IEnumerable <QuestionModel> > > putQuestion(int areaId, int courseId, int lessonId, [FromBody] QuestionModel question, int questionId)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }
                var resp = await service.putQuestionAsync(areaId, courseId, lessonId, question, questionId);

                ResponseIdModel rsp = new ResponseIdModel();
                rsp.id = resp.Id.GetValueOrDefault();
                return(Created($"api/Area/{areaId:int}/Course/{courseId:int}/Lesson/{lessonId:int}/Question/{resp.Id:int}", rsp));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
        public static T ResultEnginer <T>(this HttpResponseMessage respons, bool isSession = true) where T : class
        {
            T             result = null;
            XErrorContext?error  = null;

            /* Вынисти анализатор HttpResponse для большей наглядности и меньшего написания повторного кода
             * Date
             * Location
             * X-Upload-UploadID
             * Connection
             * X-Upload-Filename
             * X-Upload-Length
             * X-Upload-Completed-Parts
             * X-Upload-Completed
             * Last-Modified
             * Content-Length
             * Content-Type
             * X-Upload-FileGUID
             * Content - X-Upload-Error / byte[]
             * ------------------------------------
             * 2.3.6.1	Простая загрузка.
             *      HTTP/1.1 200 OK
             +Location: /ext-bus-file-store-service/rest/homemanagement/dc9441c7-312a-4210-b77f-ea368359795f
             +Date: Mon, 1 Nov 2010 20:34:56 GMT
             *
             *
             *      2.3.6.2	Загрузка частями.
             *      2.3.6.2.1	Инициализация сессии.
             *      HTTP/1.1 200 OK
             +Date: Mon, 1 Nov 2010 20:34:56 GMT
             +X-Upload-UploadID: dc9441c7-312a-4210-b77f-ea368359795f
             *
             *
             *
             *      2.3.6.2.2	Загрузка части файла.
             *      HTTP/1.1 200 OK
             +Date: Mon, 1 Nov 2010 20:34:56 GMT
             *
             *
             *      2.3.6.2.3	Завершение сессии загрузки.
             *      HTTP/1.1 200 OK
             +Date: Mon, 1 Nov 2010 20:34:56 GMT
             +Connection: close
             *
             *
             *      2.3.6.2.4	Получение сведений о загружаемом файле.
             *      HTTP/1.1 200 OK
             +Date: Mon, 1 Nov 2010 20:34:56 GMT
             +X-Upload-Filename: dogovo.tif
             +X-Upload-Length: 20000000
             +X-Upload-Completed-Parts: 1,2,3
             +X-Upload-Completed: false
             *
             *
             *      2.3.6.3	Выгрузка файла.
             *      HTTP/1.1 200 OK
             +Date: Wed, 06 Jun 2012 20:48:15 GMT
             +Last-Modified: Wed, 06 Jun 2012 13:39:25 GMT
             +Content-Length: 611892
             +Content-Type: text/plain
             +X-Upload-FileGUID: dc9441c7-312a-4210-b77f-ea368359795f
             +[1000 bytes of object data]
             */

            //разобрать HttpResponseMessage

            var typeSwitcher = new Dictionary <Type, Action>
            {
                { typeof(ResponseIdModel), () =>
                  {
                      var id = respons.StatusCode == HttpStatusCode.OK ?  isSession
                        ? respons.Headers.GetVal(HeadParam.X_Upload_UploadID.GetName())
                        : respons.Headers.Location.ToString().Split('/').LastOrDefault()
                        : null;

                      result = new ResponseIdModel
                      {
                          ResultDate  = respons.Headers.Date,
                          UploadId    = id,
                          ServerError = error
                      } as T;
                  } },
                { typeof(ResponseSessionCloseModel), () =>
                  {
                      result = new ResponseSessionCloseModel
                      {
                          ResultDate  = respons.Headers.Date,
                          IsClose     = respons.StatusCode == HttpStatusCode.OK ? respons.Headers.ConnectionClose : null,
                          ServerError = error
                      } as T;
                  } },
                { typeof(ResponseInfoModel), () =>
                  {
                      long size = 0;

                      int[] parts    = null;
                      bool? compleat = false;

                      if (respons.StatusCode == HttpStatusCode.OK)
                      {
                          long.TryParse(respons.Headers.GetVal(HeadParam.X_Upload_Length.GetName()), out size);

                          parts    = respons.Headers.GetVal(HeadParam.X_Upload_Completed_Parts.GetName()).ToParts();
                          compleat = respons.Headers.GetVal(HeadParam.X_Upload_Completed.GetName()).ToBool();
                      }

                      result = new ResponseInfoModel
                      {
                          ResultDate         = respons.Headers.Date,
                          FileName           = respons.StatusCode == HttpStatusCode.OK ?  respons.Headers.GetVal(HeadParam.X_Upload_Filename.GetName()) : null,
                          FileSize           = size,
                          FileCompleateParts = parts,
                          IsCompleate        = compleat,
                          ServerError        = error
                      } as T;
                  } },
                { typeof(ResponseDownloadModel), () =>
                  {
                      result = new ResponseDownloadModel
                      {
                          ResultDate           = respons.Headers.Date,
                          FileLastModification = respons.StatusCode == HttpStatusCode.OK ? respons.Content.Headers.LastModified : null,
                          FileType             = respons.StatusCode == HttpStatusCode.OK ? respons.Content.Headers.ContentType?.MediaType : null,
                          RFileBytes           = respons.StatusCode == HttpStatusCode.OK ? respons.Content.ReadAsByteArrayAsync().Result : null,
                          ServerError          = error
                      } as T;
                  } },
            };

            typeSwitcher[typeof(T)]();

            return(result);
        }