Exemple #1
0
 public DbDependencyResolver(DbContext context)
 {
     this.context                = context;
     this.usersRepository        = new DbUsersRepository(context);
     this.newsArticlesRepository = new DbRepository <NewsArticle>(context);
     this.commentsRepository     = new DbRepository <Comment>(context);
 }
 public DbDependencyResolver(DbContext context)
 {
     this.context = context;
     this.usersRepository = new DbUsersRepository(context);
     this.newsArticlesRepository = new DbRepository<NewsArticle>(context);
     this.commentsRepository = new DbRepository<Comment>(context);
 }
        public HttpResponseMessage LoginUser(UserModel user)
        {
            IQueryable <User> users = this.userRepository.GetAll();

            var result = from u in users
                         where u.Username == user.Username && u.Password == user.Password
                         select u;

            User newUser = result.FirstOrDefault();

            if (newUser != null)
            {
                var             rep        = new DbUsersRepository(this.db);
                var             sessionKey = rep.LoginUser(user.Username, user.Password);
                UserLoggedModel userModel  = new UserLoggedModel()
                {
                    UserID     = newUser.UserID,
                    Username   = newUser.Username,
                    SessionKey = sessionKey
                };
                var responseMsg = this.Request.CreateResponse(HttpStatusCode.OK, userModel);
                return(responseMsg);
            }
            else
            {
                var responseMsg = this.Request.CreateResponse(HttpStatusCode.NotFound);
                return(responseMsg);
            }
        }
        public static NewsArticle ToNewsArticleEntity(
            NewsArticleModel newsArticleModel,
            DbUsersRepository usersRepository,
            IRepository<Comment> commentsRepository,
            IRepository<NewsArticle> newsArticlesRepository)
        {
            NewsArticle newsArticleEntity = new NewsArticle()
                {
                    ID = newsArticleModel.ID,
                    Author = usersRepository.GetByNickname(newsArticleModel.Author),
                    Content = newsArticleModel.Content,
                    Date = newsArticleModel.Date,
                    ImageUrl = newsArticleModel.ImageUrl,
                    Rating = newsArticleModel.Rating,
                    Title = newsArticleModel.Title
                };

            foreach (CommentModel comment in newsArticleModel.Comments)
            {
                newsArticleEntity.Comments.Add(
                    Extensions.CreateOrLoadComment(
                        comment, 
                        commentsRepository, 
                        usersRepository,
                        newsArticlesRepository));
            }

            return newsArticleEntity;
        }
        public HttpResponseMessage LoginUser(UserModel user)
        {
            IQueryable<User> users = this.userRepository.GetAll();

            var result = from u in users
                         where u.Username == user.Username && u.Password == user.Password
                         select u;

            User newUser = result.FirstOrDefault();
            if (newUser != null)
            {
                var rep = new DbUsersRepository(db);
                var sessionKey = rep.LoginUser(user.Username, user.Password);
                UserLoggedModel userModel = new UserLoggedModel()
                {
                    UserID = newUser.UserID,
                    Username = newUser.Username,
                    SessionKey = sessionKey
                };
                var responseMsg = Request.CreateResponse(HttpStatusCode.OK, userModel);
                return responseMsg;
            }
            else
            {
                var responseMsg = Request.CreateResponse(HttpStatusCode.NotFound);
                return responseMsg;
            }
        }
 public NewsArticlesController(
     IRepository<NewsArticle> newsArticlesRepository,
     DbUsersRepository usersRepository,
     IRepository<Comment> commentsRepository)
 {
     this.newsArticlesRepository = newsArticlesRepository;
     this.usersRepository = usersRepository;
     this.commentsRepository = commentsRepository;
 }
 public NewsArticlesController(
     IRepository <NewsArticle> newsArticlesRepository,
     DbUsersRepository usersRepository,
     IRepository <Comment> commentsRepository)
 {
     this.newsArticlesRepository = newsArticlesRepository;
     this.usersRepository        = usersRepository;
     this.commentsRepository     = commentsRepository;
 }
        public HttpResponseMessage LogoutUser(string sessionKey)
        {
            var rep = new DbUsersRepository(this.db);

            rep.LogoutUser(sessionKey);

            var responseMsg = this.Request.CreateResponse(HttpStatusCode.OK);

            return(responseMsg);
        }
        public static Comment CreateOrLoadComment(
            CommentModel comment,
            IRepository<Comment> commentRepository,
            DbUsersRepository usersRepository)
        {
            Comment commentEntity = commentRepository.Get(comment.ID);
            if (commentEntity != null)
            {
                return commentEntity;
            }

            Comment newComment = CommentsMapper.ToCommentEntity(comment, usersRepository);

            return newComment;
        }
        public static Comment CreateOrLoadComment(
            CommentModel comment,
            IRepository <Comment> commentRepository,
            DbUsersRepository usersRepository)
        {
            Comment commentEntity = commentRepository.Get(comment.ID);

            if (commentEntity != null)
            {
                return(commentEntity);
            }

            Comment newComment = CommentsMapper.ToCommentEntity(comment, usersRepository);

            return(newComment);
        }
        public static Comment ToCommentEntity(
            CommentModel commentModel,
            DbUsersRepository usersRepository,
            IRepository<NewsArticle> newsArticlesRepository)
        {
            Comment commentEntity = new Comment()
                {
                    Content = commentModel.Content,
                    Date = commentModel.Date,
                    Author = usersRepository.GetByNickname(commentModel.Author)
                };

            NewsArticle newsArticle = newsArticlesRepository.Get(commentModel.ArticleID);
            newsArticle.Comments.Add(commentEntity);

            return commentEntity;
        }
        public HttpResponseMessage RegisterUser(User user)
        {
            var rep = new DbUsersRepository(this.db);

            if (user.Username.Length <= 3 || user.Password.Length <= 3)
            {
                return(this.Request.CreateResponse(HttpStatusCode.ExpectationFailed, "Username or password incorrect!"));
            }
            rep.CreateUser(user.Username, user.Password);

            var sessionKey = rep.LoginUser(user.Username, user.Password);

            var loggedUser = new UserLoggedModel()
            {
                UserID     = user.UserID,
                Username   = user.Username,
                SessionKey = sessionKey
            };

            return(this.Request.CreateResponse(HttpStatusCode.Created, loggedUser));
        }
        public static Comment ToCommentEntity(CommentModel commentModel, DbUsersRepository usersRepository)
        {
            Comment commentEntity = new Comment()
                {
                    ID = commentModel.ID,
                    Content = commentModel.Content,
                    Date = commentModel.Date,
                    Author = usersRepository.GetByNickname(commentModel.Author)
                };

            foreach (CommentDetails subComment in commentModel.SubComments)
            {
                commentEntity.SubComments.Add(new Comment()
                {
                    ID = subComment.ID,
                    Content = subComment.Content,
                    Date = subComment.Date,
                    Author = usersRepository.GetByNickname(subComment.Author)
                });
            }

            return commentEntity;
        }
 public object GetService(Type serviceType)
 {
     var context = new ChatContext();
     if (serviceType == typeof(PostsController))
     {
         var repository = new DbPostsRepository(context);
         return new PostsController(repository);
     }
     else if (serviceType == typeof(UsersController))
     {
         var repository = new DbUsersRepository(context);
         return new UsersController(repository);
     }
     else if (serviceType == typeof(ChatRoomsController))
     {
         var repository = new DbChatRoomsRepository(context);
         return new ChatRoomsController(repository);
     }
     else
     {
         return null;
     }
 }
        public static Comment ToCommentEntity(CommentModel commentModel, DbUsersRepository usersRepository)
        {
            Comment commentEntity = new Comment()
            {
                ID      = commentModel.ID,
                Content = commentModel.Content,
                Date    = commentModel.Date,
                Author  = usersRepository.GetByNickname(commentModel.Author)
            };

            foreach (CommentDetails subComment in commentModel.SubComments)
            {
                commentEntity.SubComments.Add(new Comment()
                {
                    ID      = subComment.ID,
                    Content = subComment.Content,
                    Date    = subComment.Date,
                    Author  = usersRepository.GetByNickname(subComment.Author)
                });
            }

            return(commentEntity);
        }
        public static NewsArticle ToNewsArticleEntity(
            NewsArticleModel newsArticleModel,
            DbUsersRepository usersRepository,
            IRepository <Comment> commentsRepository)
        {
            NewsArticle newsArticleEntity = new NewsArticle()
            {
                ID         = newsArticleModel.ID,
                Author     = usersRepository.GetByNickname(newsArticleModel.Author),
                Content    = newsArticleModel.Content,
                Date       = newsArticleModel.Date,
                ImagesUrls = newsArticleModel.ImagesUrls,
                Rating     = newsArticleModel.Rating,
                Title      = newsArticleModel.Title
            };

            foreach (CommentModel comment in newsArticleModel.Comments)
            {
                newsArticleEntity.Comments.Add(
                    Extensions.CreateOrLoadComment(comment, commentsRepository, usersRepository));
            }

            return(newsArticleEntity);
        }
Exemple #17
0
        public object GetService(Type serviceType)
        {
            var context = new ChatContext();

            if (serviceType == typeof(PostsController))
            {
                var repository = new DbPostsRepository(context);
                return(new PostsController(repository));
            }
            else if (serviceType == typeof(UsersController))
            {
                var repository = new DbUsersRepository(context);
                return(new UsersController(repository));
            }
            else if (serviceType == typeof(ChatRoomsController))
            {
                var repository = new DbChatRoomsRepository(context);
                return(new ChatRoomsController(repository));
            }
            else
            {
                return(null);
            }
        }
        public HttpResponseMessage RegisterUser(User user)
        {
            var rep = new DbUsersRepository(this.db);
            if (user.Username.Length <= 3 || user.Password.Length <= 3)
            {
                return this.Request.CreateResponse(HttpStatusCode.ExpectationFailed, "Username or password incorrect!");
            }
            rep.CreateUser(user.Username, user.Password);

            var sessionKey = rep.LoginUser(user.Username, user.Password);

            var loggedUser = new UserLoggedModel()
            {
                UserID = user.UserID,
                Username = user.Username,
                SessionKey = sessionKey,
                AvatarURL = user.Avatar
            };

            return this.Request.CreateResponse(HttpStatusCode.Created, loggedUser);
        }
        public HttpResponseMessage LogoutUser(string sessionKey)
        {
            var rep = new DbUsersRepository(this.db);
            rep.LogoutUser(sessionKey);

            var responseMsg = this.Request.CreateResponse(HttpStatusCode.OK);
            return responseMsg;
        }
        public HttpResponseMessage RegisterUser(User user)
        {
            var rep = new DbUsersRepository(db);
            rep.CreateUser(user.Username, user.Password);

            var sessionKey = rep.LoginUser(user.Username, user.Password);

            var loggedUser = new UserLoggedModel()
            {
                UserID = user.UserID,
                Username = user.Username,
                SessionKey = sessionKey
            };

            return Request.CreateResponse(HttpStatusCode.Created, loggedUser);
        }