コード例 #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);
            }
        }
コード例 #2
0
 public User GetUserByUsername(string username)
 {
     using (var db = new MishMashDbContext())
     {
         return(db.Users.FirstOrDefault(u => u.Username == username));
     }
 }
コード例 #3
0
 public string GetUserRole(string username)
 {
     using (var db = new MishMashDbContext())
     {
         return(db.Users.FirstOrDefault(u => u.Username == username).Role.ToString());
     }
 }
コード例 #4
0
        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);
            }
        }
コード例 #5
0
        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);
            }
        }
コード例 #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));
     }
 }
コード例 #7
0
 public bool IsUsernameAlreadyTaken(string username)
 {
     using (var db = new MishMashDbContext())
     {
         return(db.Users.Any(u => u.Username == username));
     }
 }
コード例 #8
0
ファイル: StartUp.cs プロジェクト: skorae/SoftUni-kdinev
 public void Configure(IServerRoutingTable serverRoutingTable)
 {
     using (var context = new MishMashDbContext())
     {
         context.Database.EnsureCreated();
     }
 }
コード例 #9
0
 public Channel GetChannelById(int channelId)
 {
     using (var db = new MishMashDbContext())
     {
         return(db.Channels.FirstOrDefault(c => c.Id == channelId));
     }
 }
コード例 #10
0
 public bool HasUser(LoginViewModel model)
 {
     using (var db = new MishMashDbContext())
     {
         return(db.Users.Any(u => u.Username == model.Username && u.Password == model.Password));
     }
 }
コード例 #11
0
        public int GetTotalFollowers(int channelId)
        {
            using (var db = new MishMashDbContext())
            {
                var followers = db.UserChannels
                                .Where(c => c.ChannelId == channelId)
                                .Count();

                return(followers);
            }
        }
コード例 #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);
            }
        }
コード例 #13
0
        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);
            }
        }
コード例 #14
0
        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();
                }
            }
        }
コード例 #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));
            }
        }
コード例 #16
0
        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();
                }
            }
        }
コード例 #17
0
        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);
            }
        }
コード例 #18
0
        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();
            }
        }
コード例 #19
0
        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();
            }
        }
コード例 #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);
            }
        }
コード例 #21
0
        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);
            }
        }
コード例 #22
0
 public HomeController(MishMashDbContext context)
     : base(context)
 {
 }
コード例 #23
0
 protected BaseController()
 {
     DbContext = new MishMashDbContext();
 }
コード例 #24
0
 public BaseController(MishMashDbContext context)
 {
     this.db = context;
 }
コード例 #25
0
ファイル: UserService.cs プロジェクト: dilyanaGl/CSharp-Web
        public UserService(MishMashDbContext context, IHashService hashService)
        {
            this.context = context;

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