예제 #1
0
        public static void Activate(string email)
        {
            var entity = new CreativeNetworkEntities();

            var userToChange = entity.User.FirstOrDefault(a => a.Email == email);
            if (userToChange != null)
                userToChange.IsBlock = false;
            entity.SaveChanges();
        }
예제 #2
0
        public static void AddChapter(int creativeId)
        {
            var entity = new CreativeNetworkEntities();
            var creative = entity.Creative.FirstOrDefault(a => a.Id == creativeId);
            int number;
            if (creative.Chapter.Select(a => a.Number).Max() == null)
            {
                number = 0;
            }
            else
            {
                number = (int) creative.Chapter.Select(a => a.Number).Max() +1;
            }

                var chapter = new Chapter
                    {
                        Text = "",
                        Name = "Chapter " + number.ToString(),
                        Number = number,

                    };
            entity.Creative.FirstOrDefault(a => a.Id == creativeId).Chapter.Add(chapter);
            entity.SaveChanges();
        }
예제 #3
0
        public static void AddUser(User user)
        {
            var entity = new CreativeNetworkEntities();

            entity.User.Add(user);
            entity.SaveChanges();
        }
예제 #4
0
        public static void AddCreative(Creative creative, string tags)
        {
            var entity = new CreativeNetworkEntities();
            if (tags != null)
            {
                string[] tagList = tags.Split(new Char[] { ' ', ',' });

                foreach (var item in tagList.Distinct())
                {
                    if (string.IsNullOrEmpty(item) || item.Length > 50) continue;
                    var tag = new Tag
                    {
                        Name = item,
                    };
                    tag.Name = tag.Name.TrimEnd().ToLower();
                    if (entity.Tag.FirstOrDefault(a => a.Name == tag.Name) != null)
                    {
                        tag = entity.Tag.FirstOrDefault(a => a.Name == tag.Name);
                    }
                    else
                    {
                        entity.Tag.Add(tag);
                    }
                    creative.Tag.Add(tag);
                    var ers = entity.GetValidationErrors();
                    entity.SaveChanges();
                }
            }
            entity.Creative.Add(creative);
            var s = entity.GetValidationErrors();
            entity.SaveChanges();
        }
예제 #5
0
        public static void SaveChapter(int id, string title, string text)
        {
            var entity = new CreativeNetworkEntities();

            var chapter = entity.Chapter.FirstOrDefault(c => c.Id == id);
            chapter.Text = text;
            chapter.Name = title;
            entity.Chapter.Attach(chapter);
            entity.Entry(chapter).State = EntityState.Modified;
            entity.SaveChanges();
        }
예제 #6
0
        public static void Rate(User user, Creative creative, int rating)
        {
            var entity = new CreativeNetworkEntities();

            var creativeLikes = entity.CreativeLikes.FirstOrDefault(a => a.UserId == user.Id && a.CreativeId == creative.Id);
            if (creativeLikes != null)
            {
                if (creativeLikes.Rating == rating)
                {
                    entity.CreativeLikes.Remove(creativeLikes);
                    entity.SaveChanges();
                    return;
                }
                else
                {
                    entity.CreativeLikes.Remove(creativeLikes);
                }
            }

            var newRate = new CreativeLikes()
            {
                UserId = user.Id,
                CreativeId = creative.Id,
                Rating = rating,
            };
            entity.CreativeLikes.Add(newRate);

            entity.SaveChanges();
        }
예제 #7
0
        public static bool MakeAdmin(int userId)
        {
            var entity = new CreativeNetworkEntities();
            var user = entity.User.FirstOrDefault(a => a.Id == userId);

            if (user.IsBlock == true) return false;
            user.RoleId = 2;

            entity.SaveChanges();
            return (user.RoleId == 2);
        }
예제 #8
0
        public static void DeleteCreative(int id)
        {
            var entity = new CreativeNetworkEntities();

            var creative = entity.Creative.FirstOrDefault(a => a.Id == id);

            creative.Tag.Clear();
            var chapters = creative.Chapter.ToArray();
            foreach (var item in chapters)
            {
                entity.Chapter.Remove(item);
            }

            creative.Chapter.Clear();
            var ratings = entity.CreativeLikes.Where(a => a.CreativeId == id).ToArray();

            foreach (var item in ratings)
            {
                entity.CreativeLikes.Remove(item);
            }
            entity.Creative.Remove(creative);
            entity.SaveChanges();

            var tags = creative.Tag.ToArray();
            foreach (var item in tags)
            {
                if (entity.Tag.FirstOrDefault(a => a.Id == item.Id).Creative.Count == 0)
                {
                    entity.Tag.Remove(item);
                }
            }
        }
예제 #9
0
        public static void DeleteChapter(int id)
        {
            var entity = new CreativeNetworkEntities();

            entity.Chapter.Remove(entity.Chapter.FirstOrDefault(a => a.Id == id));
            entity.SaveChanges();
        }
예제 #10
0
        public static void ChangePassword(int userId, string newPasswordHash)
        {
            var entity = new CreativeNetworkEntities();
            var user = entity.User.FirstOrDefault(a => a.Id == userId);

            user.Password = newPasswordHash;

            entity.SaveChanges();
        }
예제 #11
0
        public static void ChangeNumber(int chapterId, int number)
        {
            var entity = new CreativeNetworkEntities();

            var chapter = entity.Chapter.FirstOrDefault(c => c.Id == chapterId);
            chapter.Number = number;
            entity.Chapter.Attach(chapter);
            entity.Entry(chapter).State = EntityState.Modified;
            entity.SaveChanges();
        }
예제 #12
0
        public static bool Block(int userId)
        {
            var entity = new CreativeNetworkEntities();
            var user = entity.User.FirstOrDefault(a => a.Id == userId);

            user.IsBlock = !user.IsBlock;

            entity.SaveChanges();
            return (bool)user.IsBlock;
        }