//public ActionResult CheckWord(string wordName) //{ // var userId = User.Identity.GetUserId(); // wordName = wordName.ToLower(); // string tempWord = wordName.ToLower(); // char firstLetter = tempWord[0]; // char lastLetter = tempWord[tempWord.Length - 1]; // while (!Char.IsLetter(firstLetter)) // { // tempWord = tempWord.Remove(0, 1); // firstLetter = tempWord[0]; // } // while (!Char.IsLetter(lastLetter)) // { // tempWord = tempWord.Remove(tempWord.Length - 1); // lastLetter = tempWord[tempWord.Length - 1]; // } // var word = db.Words.FirstOrDefault(w=>w.Name== tempWord); // var profile = db.Profiles.FirstOrDefault(p => p.ProfileUser.Id == userId); // var profileWord = db.ProfileWords. // FirstOrDefault(pw => pw.ProfileId == profile.ProfileId && pw.WordId == word.WordId); // if (profileWord == null) // { // profileWord = new ProfileWord() // { // Profile = profile, // Word = word, // CheckedCount = 0, // LastChecked = DateTime.Now // }; // db.ProfileWords.Add(profileWord); // } // else // { // profileWord.LastChecked = DateTime.Now; // profileWord.CheckedCount += 1; // } // db.SaveChanges(); // return RedirectToAction("Index", "Stories"); //} public ActionResult Finish(int?id, int?rating) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Story story = db.Stories.Find(id); if (story == null) { return(HttpNotFound()); } var userId = User.Identity.GetUserId(); var userStory = db.ProfileStories.Where(p => p.StoryId == id && p.Profile.ProfileUser.Id == userId).FirstOrDefault(); //userStory relation if (!userStory.Read) { userStory.Read = true; userStory.Rating = rating; db.SaveChanges(); } var profile = db.Profiles.FirstOrDefault(p => p.ProfileUser.Id == userId); //userWord relations var storyWords = db.StoryWords.Where(sw => sw.StoryId == story.StoryId).Select(sw => sw.Word).ToList(); foreach (var storyWord in storyWords) { var profileWord = db.ProfileWords.FirstOrDefault(pw => pw.Profile.ProfileUser.Id == userId && pw.WordId == storyWord.WordId); if (profileWord == null) { var word = db.Words.FirstOrDefault(w => w.WordId == storyWord.WordId); profileWord = new ProfileWord() { Profile = profile, Word = word, CheckedCount = 0, LastChecked = null }; db.ProfileWords.Add(profileWord); db.SaveChanges(); } } return(RedirectToAction("Index")); }
public async Task <IHttpActionResult> DeleteProfileWord(int id) { var userId = User.Identity.GetUserId(); ProfileWord profileWord = await db.ProfileWords .Where(pw => pw.Id == id && pw.ProfileId == userId) .FirstOrDefaultAsync(); if (profileWord == null) { return(NotFound()); } db.ProfileWords.Remove(profileWord); await db.SaveChangesAsync(); return(Ok(AutoMapper.Mapper.Map <ProfileWordDTO>(profileWord))); }