Пример #1
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();
        }
Пример #2
0
        // add/update/clear search index data
        public static void AddUpdateLuceneIndex()
        {
            using (var context = new CreativeNetworkEntities())
            {

                // init lucene
                var analyzer = new StandardAnalyzer(Version.LUCENE_30);
                using (var writer = new IndexWriter(_directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
                {
                    // add data to lucene search index (replaces older entries if any)
                    foreach (var creative in context.Creative)
                    {
                        _addToLuceneIndex(creative, writer);
                        foreach (var chapter in creative.Chapter)
                        {
                            _addToLuceneIndex(chapter, writer);
                        }
                    }

                    // close handles
                    analyzer.Close();
                    writer.Dispose();
                }
            }
        }
Пример #3
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();
        }
Пример #4
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();
        }
Пример #5
0
 public static int GetTotalRates(int rating)
 {
     var entity = new CreativeNetworkEntities();
     var result = entity.CreativeLikes.Count(r => r.Rating == rating);
     return result;
 }
Пример #6
0
        public static void DeleteChapter(int id)
        {
            var entity = new CreativeNetworkEntities();

            entity.Chapter.Remove(entity.Chapter.FirstOrDefault(a => a.Id == id));
            entity.SaveChanges();
        }
Пример #7
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();
        }
Пример #8
0
        public static User GetUser(string email, string password)
        {
            var entity = new CreativeNetworkEntities();

            return entity.User.FirstOrDefault(u => u.Email == email && u.Password == password);
        }
Пример #9
0
 public static int GetUserRating(int userId)
 {
     var entity = new CreativeNetworkEntities();
     User user = entity.User.FirstOrDefault(a => a.Id == userId);
     return user.Creative.SelectMany(creative => creative.CreativeLikes).Sum(like => (int) like.Rating);
 }
Пример #10
0
        public static Creative GetCreative(int creativeId)
        {
            var entity = new CreativeNetworkEntities();

            return entity.Creative.FirstOrDefault(creative => creative.Id == creativeId);
        }
Пример #11
0
        public static Creative[] GetCreatives(User user)
        {
            var entity = new CreativeNetworkEntities();

            return entity.Creative.Where(a => a.User.Id == user.Id).ToArray();
        }
Пример #12
0
        public static User[] GetAllUsers()
        {
            var entity = new CreativeNetworkEntities();

            return entity.User.ToArray();
        }
Пример #13
0
        public static Chapter GetChapter(int chapterId)
        {
            var entity = new CreativeNetworkEntities();

            return entity.Chapter.FirstOrDefault(creative => creative.Id == chapterId);
        }
Пример #14
0
        public static Tag[] GetAllTags()
        {
            var entity = new CreativeNetworkEntities();

            return entity.Tag.ToArray();
        }
Пример #15
0
        public static Creative[] GetAllCreatives()
        {
            var entity = new CreativeNetworkEntities();

            return entity.Creative.ToArray();
        }
Пример #16
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);
                }
            }
        }
Пример #17
0
        public static int GetTotalRating(int creativeId)
        {
            var entity = new CreativeNetworkEntities();

            Creative image = entity.Creative.FirstOrDefault(a => a.Id == creativeId);
            CreativeLikes[] ratings = image.CreativeLikes.Where(a => a.CreativeId == creativeId).ToArray();

            return ratings.Sum(item => (int) item.Rating);
        }
Пример #18
0
        public static Creative[] GetCreatives(Tag tag)
        {
            var entity = new CreativeNetworkEntities();

            return entity.Creative.Where(a => a.Tag.FirstOrDefault(t => t.Name == tag.Name) != null).ToArray();
        }
Пример #19
0
        public static User GetUser(int id)
        {
            var entity = new CreativeNetworkEntities();

            return entity.User.FirstOrDefault(a => a.Id == id);
        }
Пример #20
0
        public static int GetRating(int creativeId, int userId)
        {
            var entity = new CreativeNetworkEntities();

            Creative creative = entity.Creative.FirstOrDefault(a => a.Id == creativeId);
            CreativeLikes creativeLikes = creative.CreativeLikes.FirstOrDefault(a => a.CreativeId == creativeId && a.UserId == userId);
            if (creativeLikes == null)
            {
                return 0;
            }
            else
            {
                return (int)creativeLikes.Rating;
            }
        }
Пример #21
0
        public static User GetUser(string cookies)
        {
            var entity = new CreativeNetworkEntities();

            return entity.User.FirstOrDefault(u => u.Cookies == cookies);
        }
Пример #22
0
        public static Tag GetTag(string tagName)
        {
            var entity = new CreativeNetworkEntities();

            return entity.Tag.FirstOrDefault(tag => tag.Name.Trim() == tagName.Trim());
        }
Пример #23
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);
        }
Пример #24
0
        public static void AddUser(User user)
        {
            var entity = new CreativeNetworkEntities();

            entity.User.Add(user);
            entity.SaveChanges();
        }
Пример #25
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();
        }
Пример #26
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();
        }
Пример #27
0
        public static int GetTagEnterance(string tagName)
        {
            var entity = new CreativeNetworkEntities();

            var creatives = entity.Tag.FirstOrDefault(a => a.Name.Trim() == tagName.Trim());
            return creatives != null ? creatives.Creative.ToArray().Length : 0;
        }
Пример #28
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();
        }
Пример #29
0
        public static Creative[] GetTopCreatives()
        {
            var entity = new CreativeNetworkEntities();

            var sortedCreatives =
                from n in entity.Creative.ToArray()
                orderby (GetCreativeRating(n.Id)) descending
                select n;

            return sortedCreatives.ToArray();
        }
Пример #30
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;
        }