Beispiel #1
0
        public Channel AddChannel(string name, string description, string tagsToString, string type)
        {
            using (var db = new MishMashDbContext())
            {
                var channel = new Channel
                {
                    Name        = name,
                    Description = description,
                    Type        = Enum.Parse <Type>(type)
                };

                db.Channels.Add(channel);
                db.SaveChanges();

                var tags = tagsToString.Split(new[] { ' ', ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
                var list = new List <Tag>();
                foreach (var tagString in tags)
                {
                    var tag = new Tag
                    {
                        Name      = tagString,
                        ChannelId = channel.Id
                    };
                    list.Add(tag);
                }

                db.Tags.AddRange(list);
                db.SaveChanges();

                return(channel);
            }
        }
Beispiel #2
0
 public User GetUserByUsername(string username)
 {
     using (var db = new MishMashDbContext())
     {
         return(db.Users.FirstOrDefault(u => u.Username == username));
     }
 }
 public string GetUserRole(string username)
 {
     using (var db = new MishMashDbContext())
     {
         return(db.Users.FirstOrDefault(u => u.Username == username).Role.ToString());
     }
 }
        public IEnumerable <ChannelViewModel> GetSuggestedChannels(string username)
        {
            using (var db = new MishMashDbContext())
            {
                int[] followedChannelsTags = db.Channels
                                             .Where(c => c.Followers.Any(uc => uc.User.Username == username))
                                             .SelectMany(c => c.Tags.Select(tc => tc.Tag.Id))
                                             .ToArray();

                ChannelViewModel[] channels = db.Channels
                                              .Where(c =>
                                                     c.Followers.All(uc => uc.User.Username != username) &&
                                                     c.Tags.Any(tc => followedChannelsTags.Contains(tc.TagId)))
                                              .Select(c => new ChannelViewModel
                {
                    Id             = c.Id,
                    Name           = c.Name,
                    Type           = c.Type.ToString(),
                    FollowersCount = c.Followers.Count()
                })
                                              .ToArray();

                return(channels);
            }
        }
        public IEnumerable <ChannelViewModel> GetOtherChannels(string username)
        {
            using (var db = new MishMashDbContext())
            {
                int[] followedChannelsIds = this.GetFollowedChannels(username)
                                            .Select(m => m.Id)
                                            .ToArray();

                int[] suggestedChannelsIds = this.GetSuggestedChannels(username)
                                             .Select(m => m.Id)
                                             .ToArray();

                int[] ids = followedChannelsIds.Concat(suggestedChannelsIds)
                            .Distinct()
                            .ToArray();

                ChannelViewModel[] channels = db.Channels
                                              .Where(c => !ids.Contains(c.Id))
                                              .Select(c => new ChannelViewModel
                {
                    Id             = c.Id,
                    Name           = c.Name,
                    Type           = c.Type.ToString(),
                    FollowersCount = c.Followers.Count()
                })
                                              .ToArray();

                return(channels);
            }
        }
Beispiel #6
0
 public User GetUserByUsernameAndPassword(string username, string password)
 {
     using (var db = new MishMashDbContext())
     {
         return(db.Users.FirstOrDefault(u => u.Username == username && u.Password == password));
     }
 }
 public bool IsUsernameAlreadyTaken(string username)
 {
     using (var db = new MishMashDbContext())
     {
         return(db.Users.Any(u => u.Username == username));
     }
 }
Beispiel #8
0
 public void Configure(IServerRoutingTable serverRoutingTable)
 {
     using (var context = new MishMashDbContext())
     {
         context.Database.EnsureCreated();
     }
 }
Beispiel #9
0
 public Channel GetChannelById(int channelId)
 {
     using (var db = new MishMashDbContext())
     {
         return(db.Channels.FirstOrDefault(c => c.Id == channelId));
     }
 }
 public bool HasUser(LoginViewModel model)
 {
     using (var db = new MishMashDbContext())
     {
         return(db.Users.Any(u => u.Username == model.Username && u.Password == model.Password));
     }
 }
Beispiel #11
0
        public int GetTotalFollowers(int channelId)
        {
            using (var db = new MishMashDbContext())
            {
                var followers = db.UserChannels
                                .Where(c => c.ChannelId == channelId)
                                .Count();

                return(followers);
            }
        }
Beispiel #12
0
        public List <Channel> GetYourChannels(int userId)
        {
            using (var db = new MishMashDbContext())
            {
                var channels = db.UserChannels
                               .Where(uc => uc.UserId == userId)
                               .Include(uc => uc.Channel)
                               .Select(uc => uc.Channel)
                               .ToList();

                return(channels);
            }
        }
        public IEnumerable <MyChannelViewModel> GetMyFollowedChannels(string username)
        {
            using (var db = new MishMashDbContext())
            {
                MyChannelViewModel[] channels = db.Channels
                                                .Where(c => c.Followers.Any(uc => uc.User.Username == username))
                                                .Select(c => new MyChannelViewModel
                {
                    Name           = c.Name,
                    Type           = c.Type.ToString(),
                    FollowersCount = c.Followers.Count()
                })
                                                .ToArray();

                return(channels);
            }
        }
        public void UnfollowChannel(int channelId, string username)
        {
            using (var db = new MishMashDbContext())
            {
                int userId = db.Users.FirstOrDefault(u => u.Username == username).Id;

                UserChannel userChannel = db.UserChannels
                                          .FirstOrDefault(uc => uc.UserId == userId && uc.ChannelId == channelId);

                if (userChannel != null)
                {
                    db.UserChannels.Remove(userChannel);

                    db.SaveChanges();
                }
            }
        }
Beispiel #15
0
        public string GetAllTags(int channelId)
        {
            using (var db = new MishMashDbContext())
            {
                var tags = db.Tags
                           .Where(t => t.ChannelId == channelId)
                           .ToList();

                var list = new List <string>();
                foreach (var tag in tags)
                {
                    list.Add(tag.Name);
                }

                return(string.Join(", ", list));
            }
        }
        public void FollowChannel(int channelId, string username)
        {
            using (var db = new MishMashDbContext())
            {
                int userId = db.Users.FirstOrDefault(u => u.Username == username).Id;

                if (!db.UserChannels.Any(uc => uc.UserId == userId && uc.ChannelId == channelId))
                {
                    db.UserChannels.Add(new UserChannel
                    {
                        UserId    = userId,
                        ChannelId = channelId
                    });

                    db.SaveChanges();
                }
            }
        }
        public ChannelDetailsViewModel GetChannel(int id)
        {
            using (var db = new MishMashDbContext())
            {
                ChannelDetailsViewModel model = db.Channels.Where(c => c.Id == id)
                                                .Select(c => new ChannelDetailsViewModel
                {
                    Name           = c.Name,
                    Description    = c.Description,
                    Tags           = string.Join(", ", c.Tags.Select(ct => ct.Tag.Name)),
                    Type           = c.Type.ToString(),
                    FollowersCount = c.Followers.Count()
                })
                                                .FirstOrDefault();

                return(model);
            }
        }
        public void AddUser(RegisterViewModel model)
        {
            using (var db = new MishMashDbContext())
            {
                Role role = db.Users.Any() ? Role.User : Role.Admin;

                var user = new User
                {
                    Username = model.Username,
                    Password = model.Password,
                    Email    = model.Email,
                    Role     = role
                };

                db.Users.Add(user);
                db.SaveChanges();
            }
        }
        public void AddChannel(ChannelDetailsViewModel model)
        {
            using (var db = new MishMashDbContext())
            {
                var channel = new Channel
                {
                    Name        = model.Name,
                    Description = model.Description,
                    Type        = Enum.Parse <ChannelType>(model.Type)
                };

                if (!string.IsNullOrWhiteSpace(model.Tags))
                {
                    string[] tags = model.Tags.Split(new[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);

                    foreach (string tag in tags)
                    {
                        Tag dbTag = db.Tags.FirstOrDefault(t => t.Name == tag);

                        if (dbTag == null)
                        {
                            dbTag = new Tag {
                                Name = tag
                            };

                            db.Tags.Add(dbTag);
                            db.SaveChanges();
                        }

                        channel.Tags.Add(new TagChannel {
                            TagId = dbTag.Id
                        });
                    }
                }

                db.Channels.Add(channel);
                db.SaveChanges();
            }
        }
Beispiel #20
0
        public User AddUser(string username, string password, string email)
        {
            using (var db = new MishMashDbContext())
            {
                var user = new User
                {
                    Username = username,
                    Password = password,
                    Email    = email,
                    Role     = Role.User
                };

                if (!db.Users.Any())
                {
                    user.Role = Role.Admin;
                }

                db.Users.Add(user);
                db.SaveChanges();

                return(user);
            }
        }
        public IEnumerable <FollowedChannelViewModel> GetFollowedChannels(string username)
        {
            using (var db = new MishMashDbContext())
            {
                FollowedChannelViewModel[] channels = db.Channels
                                                      .Where(c => c.Followers.Any(uc => uc.User.Username == username))
                                                      .Select(c => new FollowedChannelViewModel
                {
                    Id             = c.Id,
                    Name           = c.Name,
                    Type           = c.Type.ToString(),
                    FollowersCount = c.Followers.Count()
                })
                                                      .ToArray();

                for (int i = 0; i < channels.Length; i++)
                {
                    channels[i].Index = i + 1;
                }

                return(channels);
            }
        }
Beispiel #22
0
 public HomeController(MishMashDbContext context)
     : base(context)
 {
 }
Beispiel #23
0
 protected BaseController()
 {
     DbContext = new MishMashDbContext();
 }
Beispiel #24
0
 public BaseController(MishMashDbContext context)
 {
     this.db = context;
 }
Beispiel #25
0
        public UserService(MishMashDbContext context, IHashService hashService)
        {
            this.context = context;

            this.hashService = hashService;
        }
Beispiel #26
0
 public UsersService(MishMashDbContext context)
 {
     this.context = context;
 }
Beispiel #27
0
 public ChannelsService(MishMashDbContext db)
     : base(db)
 {
 }
Beispiel #28
0
 public ChannelService()
 {
     this.context = new MishMashDbContext();
 }
 public ChannelsService(MishMashDbContext context)
 {
     this.context = context;
 }
Beispiel #30
0
 public BaseController()
 {
     this.Db = new MishMashDbContext();
 }