public ActionResult GetStory(int id)
        {
            StoryData sd = new StoryData();
            StoryModel sm = new StoryModel();
            sm = convert.StoryDataToModel(sd.GetStory(id));

            return PartialView("Story",sm);
        }
Esempio n. 2
0
 public StoryData GetStory(int storyID)
 {
     var story = db.Stories.AsNoTracking().Where(x => x.id == storyID).FirstOrDefault();
     StoryData sm = new StoryData()
     {
         id = story.id,
         Description = story.Description,
         PostedOn = story.PostedOn,
         StoryContent = story.StoryContent,
         Title = story.Title,
     };
     return sm;
 }
        public ActionResult AddEditStory(int? id)
        {
            StoryData sd = new StoryData();
            StoryModel sm = new StoryModel();
            if (id != null)
            {
                sm = convert.StoryDataToModel( sd.GetStory(id.Value));
            }

            GroupData gd = new GroupData();
            var gds = gd.GetGroups(id);
            List<GroupModel> grModel = new List<GroupModel>();
            foreach (var item in gds)
            {
                grModel.Add(convert.GroupDataToModel(item));
            }

            sm.Groups = grModel;

            return View(sm);
        }
        public ActionResult StorySave(StoryModel model)
        {
            model.UserID = Convert.ToInt32(User.Identity.Name);
            StoryData sd = new StoryData();
            sd = convert.StoryModelToData(model);
            var result = sd.SaveStory(sd);

            string action ;
            if (result)
            {
                 action = "Stories";
            }
            else
            {
                 action = "AddEditStory";
            }

            return RedirectToAction(action);
        }
        public ActionResult Stories()
        {
            StoryData sd = new StoryData();
            var userID = Convert.ToInt32(User.Identity.Name);
            List<StoryModel> stories =   new List<StoryModel>();
            List<StoryData>  sds = sd.GetStories(userID);
            foreach (var item in sds)
            {
                stories.Add(convert.StoryDataToModel(item));
            }

            return View(stories);
        }
        //public int CheckLogin(LoginModel lm)
        //{
        //    var userid = db.Users.Where(x => x.name == lm.Login && x.password == lm.Password).Select(s=>s.id).FirstOrDefault();
        //    return userid;
        //}
        //public bool SaveStory(StoryModel sm)
        //{
        //    try
        //    {
        //        Story story;
        //        if (sm.id == null)
        //        {
        //            story = new Story
        //            {
        //                UsersID = sm.UserID,
        //                Description = sm.Description,
        //                StoryContent = sm.StoryContent,
        //                Title = sm.Title,
        //                PostedOn = DateTime.Now
        //            };
        //            db.Stories.Add(story);
        //        }
        //        else
        //        {
        //            story = db.Stories.Where(x => x.id == sm.id).FirstOrDefault();
        //            story.UsersID = sm.UserID;
        //            story.Description = sm.Description;
        //            story.StoryContent = sm.StoryContent;
        //            story.Title = sm.Title;
        //        }
        //        var SelectedGroupIDs = sm.Groups.Where(x => x.isSelected == true).Select(s => s.id).ToArray();
        //        List<Group> gr = db.Groups.Where(x => SelectedGroupIDs.Contains(x.id)).ToList();
        //        story.Groups.Clear();
        //        foreach (var item in gr)
        //        {
        //            story.Groups.Add(item);
        //        }
        //        db.SaveChanges();
        //        return true;
        //    }
        //    catch (Exception)
        //    {
        //        return false;
        //    }
        //}
        //public List<StoryModel> GetStories(int userID)
        //{
        //    var stories = db.Stories.Where(x => x.UsersID == userID).ToList();
        //    List<StoryModel> sm = new List<StoryModel>();
        //    foreach (var item in stories)
        //    {
        //        sm.Add(new StoryModel()
        //        {
        //            id = item.id,
        //            Description = item.Description,
        //            PostedOn = item.PostedOn,
        //            StoryContent = item.StoryContent,
        //            Title = item.Title
        //        });
        //    }
        //    return sm;
        //}
        //public StoryModel GetStory(int storyID)
        //{
        //    var story = db.Stories.Where(x => x.id == storyID).FirstOrDefault();
        //    StoryModel sm = new StoryModel()
        //    {
        //        id = story.id,
        //        Description = story.Description,
        //        PostedOn = story.PostedOn,
        //        StoryContent = story.StoryContent,
        //        Title = story.Title,
        //    };
        //    return sm;
        //}
        //public List<GroupModel> GetGroups(int? id)
        //{
        //    var groups = db.Groups.ToList();
        //    List<Group> selected = new List<Group>();
        //    if (id != null)
        //    {
        //        selected = db.Groups.Where(u => u.Stories.Any(e => e.id == id.Value)).ToList() ;
        //    }
        //    List<GroupModel> gms = new List<GroupModel>();
        //    foreach (var item in groups)
        //    {
        //        bool isselected = selected.Where(s => s.id == item.id).ToList().Count > 0;
        //        GroupModel gm = new GroupModel()
        //        {
        //            id = item.id,
        //            Description = item.Description,
        //            Name = item.Name,
        //            isSelected = isselected
        //        };
        //        gms.Add(gm);
        //    }
        //    return gms;
        //}
        //public List<GroupModel> GetGroupsInfo()
        //{
        //    var groups = db.Groups.ToList();
        //    List<GroupModel> gms = new List<GroupModel>();
        //    foreach (var item in groups)
        //    {
        //        var usersCount = item.Stories.GroupBy(s => s.UsersID).Select(g=>g.Count()).ToList().Count();
        //        GroupModel gm = new GroupModel()
        //        {
        //            id = item.id,
        //            Description = item.Description,
        //            Name = item.Name,
        //            StoriesCount = item.Stories.Count,
        //            UsersCount = usersCount
        //        };
        //        gms.Add(gm);
        //    }
        //    return gms;
        //}
        /// <summary>
        /// StoryModel  convert to StoryData 
        /// </summary>
        /// <param name="sm"></param>
        /// <returns></returns>
        public StortyBack.Data.StoryData StoryModelToData(StoryModel sm)
        {
            List<GroupData> gd = new List<GroupData>();
            foreach (var group in sm.Groups)
            {
                gd.Add(GroupModelToData(group));
            }

            StortyBack.Data.StoryData sd = new StortyBack.Data.StoryData()
            {
                id = sm.id,
                PostedOn = sm.PostedOn,
                StoryContent = sm.StoryContent,
                Title = sm.Title,
                Description = sm.Description,
                UserID = sm.UserID,
                Groups = gd
            };
            return sd;
        }
Esempio n. 7
0
        public bool SaveStory(StoryData sm)
        {
            try
            {
                Story story;
                if (sm.id == null)
                {
                    story = new Story
                    {
                        UsersID = sm.UserID,
                        Description = sm.Description,
                        StoryContent = sm.StoryContent,
                        Title = sm.Title,
                        PostedOn = DateTime.Now
                    };
                    db.Stories.Add(story);
                }
                else
                {
                    story = db.Stories.Where(x => x.id == sm.id).FirstOrDefault();
                    story.UsersID = sm.UserID;
                    story.Description = sm.Description;
                    story.StoryContent = sm.StoryContent;
                    story.Title = sm.Title;
                }

                var SelectedGroupIDs = sm.Groups.Where(x => x.isSelected == true).Select(s => s.id).ToArray();
                List<Group> gr = db.Groups.Where(x => SelectedGroupIDs.Contains(x.id)).ToList();
                story.Groups.Clear();
                foreach (var item in gr)
                {
                    story.Groups.Add(item);
                }

                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }