Exemplo n.º 1
0
        public async Task <IHttpActionResult> DeleteLearnRequest(int id)
        {
            var learnRequest = await db.LearnRequests.FindAsync(id);

            if (learnRequest == null)
            {
                var message    = "Can't find the learn request.";
                var modelError = EDCExceptionFactory.GenerateHttpError(message, EDCWebServiceErrorType.Error, true);
                var response   = Request.CreateErrorResponse(HttpStatusCode.NotFound, modelError);
                throw new HttpResponseException(response);
            }
            try
            {
                db.LearnRequests.Remove(learnRequest);
                await db.SaveChangesToDbAsync();
            }
            catch (Exception e)
            {
                var msg        = e.Message;
                var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true);
                var response   = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, modelError);
                throw new HttpResponseException(response);
            }
            return(Ok());
        }
Exemplo n.º 2
0
        public async Task <IHttpActionResult> PutWord([FromBody] int id)
        {
            var word = await db.Words.Include(w => w.Students)
                       .Where(p => p.ID == id).SingleOrDefaultAsync();

            if (word == null)
            {
                var msg        = String.Format("The word can't be find.");
                var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true);
                var response   = Request.CreateErrorResponse(HttpStatusCode.NotFound, modelError);
                throw new HttpResponseException(response);
            }
            var student = await db.Students.FindAsync(User.Identity.Name);

            var wordStudent = word.Students.Where(s => s.StudentName == User.Identity.Name).SingleOrDefault();

            if (wordStudent != null)
            {
                return(Ok());
            }

            try
            {
                word.Students.Add(student);
                await db.SaveChangesToDbAsync();
            }
            catch (Exception e)
            {
                var msg        = e.Message;
                var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true);
                var response   = Request.CreateErrorResponse(HttpStatusCode.NotFound, modelError);
                throw new HttpResponseException(response);
            }
            return(Ok());
        }
Exemplo n.º 3
0
        //     [ResponseType(typeof(EDCWordDTO))]
        public async Task <IHttpActionResult> PostWord(AddWordBindingModel word)
        {
            if (!ModelState.IsValid)
            {
                var msg        = "Something wrong with the input while adding the word.";
                var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true);
                var response   = Request.CreateErrorResponse(HttpStatusCode.BadRequest, modelError);
                throw new HttpResponseException(response);
            }
            //to see if the word is already there
            var wordFromDb = await db.Words.Where(p => p.Character == word.Character).SingleOrDefaultAsync();

            if (wordFromDb != null)
            {
                return(Ok());
            }
            //see if the date is the same
            var dateFromDb = await db.Words.Where(p => p.Date == word.Date).SingleOrDefaultAsync();

            if (dateFromDb != null)
            {
                var msg        = String.Format("The date {0} already has the word.", word.Date);
                var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true);
                var response   = Request.CreateErrorResponse(HttpStatusCode.BadRequest, modelError);
                throw new HttpResponseException(response);
            }
            var edcWord = new EDCWord()
            {
                Audio         = word.Audio,
                Pinyin        = word.Pinyin,
                Character     = word.Character,
                BasicMeanings = word.BasicMeanings,
                Date          = word.Date,
                Explanation   = word.Explanation
            };

            if (word.Phrases != null && word.Phrases.Count() > 0)
            {
                var phrases  = new List <EDCPhrase>();
                var examples = new List <EDCPhraseExample>();
                foreach (var p in word.Phrases)
                {
                    if (p.English == null || p.English.Length == 0 ||
                        p.Chinese == null || p.Chinese.Length == 0)
                    {
                        continue;
                    }
                    var phraseExamples = new List <EDCPhraseExample>();
                    foreach (var e in p.Examples)
                    {
                        phraseExamples.Add(new EDCPhraseExample
                        {
                            Englisgh = e.English,
                            Chinese  = e.Chinese
                        });
                    }
                    phrases.Add(new EDCPhrase
                    {
                        English  = p.English,
                        Chinese  = p.Chinese,
                        Pinyin   = p.Pinyin,
                        Examples = phraseExamples
                    });
                    examples.AddRange(phraseExamples);
                }
                edcWord.Phrases = phrases;
                foreach (var e in examples)
                {
                    db.PhraseExamples.Add(e);
                }
                foreach (var p in phrases)
                {
                    db.Phrases.Add(p);
                }
            }
            if (word.Slangs != null && word.Slangs.Count() > 0)
            {
                var slangs = new List <EDCSlang>();
                foreach (var q in word.Slangs)
                {
                    if (q.SlangChinese == null || q.SlangChinese.Length == 0 ||
                        q.SlangEnglish == null || q.SlangEnglish.Length == 0 ||
                        q.SlangExampleChinese == null || q.SlangExampleChinese.Length == 0 ||
                        q.SlangExampleEnglish == null || q.SlangExampleEnglish.Length == 0)
                    {
                        continue;
                    }
                    slangs.Add(new EDCSlang
                    {
                        SlangChinese        = q.SlangChinese,
                        SlangEnglish        = q.SlangEnglish,
                        SlangExampleEnglish = q.SlangExampleEnglish,
                        SlangExampleChinese = q.SlangExampleChinese
                    });
                }
                edcWord.Slangs = slangs;
                foreach (var q in slangs)
                {
                    db.Slangs.Add(q);
                }
            }

            db.Words.Add(edcWord);
            try
            {
                await db.SaveChangesToDbAsync();

                return(Ok());
            }
            catch (Exception)
            {
                var msg        = "Something internal errors occurred while adding the word.";
                var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true);
                var response   = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, modelError);
                throw new HttpResponseException(response);
            }
            //var wordDto = db.GenerateDTO(edcWord);
            //return CreatedAtRoute("DefaultApi", new { id = edcWord.ID }, wordDto);
        }
Exemplo n.º 4
0
        public async Task <IHttpActionResult> PostScene(AddSceneBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                var msg        = "Something wrong with the input while adding the scenario.";
                var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true);
                var response   = Request.CreateErrorResponse(HttpStatusCode.BadRequest, modelError);
                throw new HttpResponseException(response);
            }

            var dateFromDb = await db.Scenarios.Where(p => p.Date == model.Date).SingleOrDefaultAsync();

            if (dateFromDb != null)
            {
                var msg        = String.Format("The date {0} already has the word.", model.Date);
                var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true);
                var response   = Request.CreateErrorResponse(HttpStatusCode.BadRequest, modelError);
                throw new HttpResponseException(response);
            }
            FilterBindingModel(model);
            var images = new List <EDCScenarioImage>();

            if (model.Images != null)
            {
                foreach (var i in model.Images)
                {
                    var words = new List <EDCScenarioWord>();
                    foreach (var w in i.SceneWords)
                    {
                        var word = new EDCScenarioWord
                        {
                            ChineseWord       = w.WordChinese,
                            ChineseWordPinyin = w.WordPinyin,
                            ChineseWordAudio  = w.WordAudio
                        };
                        words.Add(word);
                        db.ScenarioWords.Add(word);
                    }
                    var image = new EDCScenarioImage
                    {
                        Image = i.ImageSrc,
                        Words = words
                    };
                    images.Add(image);
                    db.ScenarioImages.Add(image);
                }
            }
            var scenario = new EDCScenarioContent
            {
                ThemeChinese = model.TitleChinese,
                ThemeEnglish = model.TitleEnglish,
                Date         = model.Date,
                Images       = images
            };

            db.Scenarios.Add(scenario);
            try
            {
                await db.SaveChangesToDbAsync();

                return(Ok());
            }
            catch (Exception)
            {
                var msg        = "Something internal errors occurred while adding the scenario.";
                var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true);
                var response   = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, modelError);
                throw new HttpResponseException(response);
            }
        }