Exemplo n.º 1
0
        public async Task <IHttpActionResult> GetTeacher(string id)
        {
            var teacher = await db.Teachers.Include(p => p.LearnRequests.Select(x => x.RegisteredStudents))
                          .SingleOrDefaultAsync(p => p.TeacherName == id);

            if (teacher == null)
            {
                var msg        = string.Format("Can't find the teacher {0}", id);
                var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true);
                var response   = Request.CreateErrorResponse(HttpStatusCode.NotFound, modelError);
                throw new HttpResponseException(response);
            }
            var learnRequestDtos = new List <EDCLearnRequestDTO>();

            foreach (var i in teacher.LearnRequests)
            {
                var dto = db.GenerateDTO(i);
                learnRequestDtos.Add(dto);
            }
            return(Ok(new EDCTeacherDTO()
            {
                TeacherName = id,
                LearnRequests = learnRequestDtos
            }));
        }
Exemplo n.º 2
0
        public async Task <IHttpActionResult> GetScene(string date)
        {
            string d;

            TimeConversionUtils.GetDate(date, out d);
            if (d == null)
            {
                var msg        = "The input date is not valid.";
                var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true);
                var response   = Request.CreateErrorResponse(HttpStatusCode.BadRequest, modelError);
                throw new HttpResponseException(response);
            }

            var scene = await db.Scenarios
                        .Include(p => p.Images.Select(x => x.Words))
                        .Where(p => p.Date == d).SingleOrDefaultAsync();

            if (scene != null)
            {
                var dto = db.GenerateDTO(scene);
                if (dto != null)
                {
                    return(Ok(dto));
                }
            }
            var message      = string.Format("Can't find the scenario for {0}.", d);
            var error        = EDCExceptionFactory.GenerateHttpError(message, EDCWebServiceErrorType.Error, true);
            var httpResponse = Request.CreateErrorResponse(HttpStatusCode.NotFound, error);

            throw new HttpResponseException(httpResponse);
        }
Exemplo n.º 3
0
        public async Task <IHttpActionResult> GetWord(string date)
        {
            if (date == null || date.Length == 0)
            {
                var msg        = "The input date is empty.";
                var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true);
                var response   = Request.CreateErrorResponse(HttpStatusCode.BadRequest, modelError);
                throw new HttpResponseException(response);
            }
            string d;

            TimeConversionUtils.GetDate(date, out d);
            if (d == null)
            {
                var msg        = "The input date is not valid.";
                var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true);
                var response   = Request.CreateErrorResponse(HttpStatusCode.BadRequest, modelError);
                throw new HttpResponseException(response);
                //return BadRequest();
            }

            var word = await db.Words
                       .Include(p => p.Phrases.Select(x => x.Examples))
                       .Include(p => p.Slangs)
                       .Where(p => p.Date == d).SingleOrDefaultAsync();

            if (word != null)
            {
                EDCWordDTO wordDto = db.GenerateDTO(word);
                if (wordDto != null)
                {
                    return(Ok(wordDto));
                }
            }
            var message      = string.Format("Can't find the word for {0}.", d);
            var error        = EDCExceptionFactory.GenerateHttpError(message, EDCWebServiceErrorType.Error, true);
            var httpResponse = Request.CreateErrorResponse(HttpStatusCode.NotFound, error);

            throw new HttpResponseException(httpResponse);
        }
Exemplo n.º 4
0
        public async Task <IHttpActionResult> GetStudent(string id)
        {
            var student = await db.Students.Include(p => p.LearnRequests)
                          .Include(p => p.Scenarios.Select(x => x.Images))
                          .Include(p => p.Words.Select(x => x.Slangs))
                          .Include(p => p.Words.Select(x => x.Phrases.Select(t => t.Examples)))
                          .Include(p => p.Scenarios.Select(x => x.Images.Select(t => t.Words)))
                          .Where(x => x.StudentName == id).SingleOrDefaultAsync();

            if (student == null)
            {
                var msg        = "Couldn't find the student.";
                var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true);
                var response   = Request.CreateErrorResponse(HttpStatusCode.BadRequest, modelError);
                throw new HttpResponseException(response);
            }
            try
            {
                var learnRequests = student.LearnRequests != null ? new List <EDCLearnRequestDTO>() : null;
                if (learnRequests != null)
                {
                    foreach (var l in student.LearnRequests)
                    {
                        var dto = db.GenerateDTO(l);
                        learnRequests.Add(dto);
                    }
                }

                var scenarios = student.Scenarios != null ? new List <EDCScenarioContentDTO>() : null;
                if (scenarios != null)
                {
                    foreach (var s in student.Scenarios)
                    {
                        var dto = db.GenerateDTO(s);
                        scenarios.Add(dto);
                    }
                }

                var words = student.Words != null ? new List <EDCWordDTO>() : null;
                if (words != null)
                {
                    foreach (var w in student.Words)
                    {
                        var dto = db.GenerateDTO(w);
                        words.Add(dto);
                    }
                }

                EDCStudentDTO studentDto = new EDCStudentDTO()
                {
                    Name          = student.StudentName,
                    LearnRequests = learnRequests,
                    Scenarios     = scenarios,
                    Words         = words
                };
                return(Ok(studentDto));
            }
            catch (Exception e)
            {
                var msg        = e.Message;
                var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true);
                var response   = Request.CreateErrorResponse(HttpStatusCode.BadRequest, modelError);
                throw new HttpResponseException(response);
            }
        }