public static EDCWordDTO GenerateDTO(this IEDCLoginUserContext context, EDCWord word) { if (context == null || word == null) { return(null); } var phrases = new List <Phrase>(); if (word.Phrases != null) { foreach (var p in word.Phrases) { var examples = new List <PhraseExample>(); if (p.Examples != null) { foreach (var e in p.Examples) { examples.Add(new PhraseExample() { English = e.Englisgh, Chinese = e.Chinese }); } } phrases.Add(new Phrase() { Chinese = p.Chinese, English = p.English, Pinyin = p.Pinyin, Examples = examples }); } } var slangs = new List <Slang>(); if (word.Slangs != null) { foreach (var q in word.Slangs) { slangs.Add(new Slang { SlangChinese = q.SlangChinese, SlangEnglish = q.SlangEnglish, SlangExampleChinese = q.SlangExampleChinese, SlangExampleEnglish = q.SlangExampleEnglish }); } } return(new EDCWordDTO() { Id = word.ID, Audio = word.Audio, BasicMeanings = word.BasicMeanings, Pinyin = word.Pinyin, Character = word.Character, Explanation = word.Explanation, Date = word.Date, Phrases = phrases, Slangs = slangs }); }
// [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); }