From 711822b88a73ae59105dafa42f84b3ab4177ba3b Mon Sep 17 00:00:00 2001 From: ezequielpirrotta Date: Thu, 13 Feb 2020 11:15:45 -0300 Subject: [PATCH] poniendo comments --- Tuiter/src/Controllers/CommentController.php | 0 Tuiter/src/Models/Comment.php | 34 ++++++++++ Tuiter/src/Models/CommentNull.php | 34 ++++++++++ Tuiter/src/Services/CommentService.php | 67 ++++++++++++++++++++ Tuiter/tests/Services/CommentServiceTest.php | 58 +++++++++++++++++ 5 files changed, 193 insertions(+) create mode 100644 Tuiter/src/Controllers/CommentController.php create mode 100644 Tuiter/src/Models/Comment.php create mode 100644 Tuiter/src/Models/CommentNull.php create mode 100644 Tuiter/src/Services/CommentService.php create mode 100644 Tuiter/tests/Services/CommentServiceTest.php diff --git a/Tuiter/src/Controllers/CommentController.php b/Tuiter/src/Controllers/CommentController.php new file mode 100644 index 0000000..e69de29 diff --git a/Tuiter/src/Models/Comment.php b/Tuiter/src/Models/Comment.php new file mode 100644 index 0000000..66f0c1e --- /dev/null +++ b/Tuiter/src/Models/Comment.php @@ -0,0 +1,34 @@ +content=$content; + $this->idOwner=$idOwner; + $this->time=$time; + $this->idPost=$idPost; + $this->idComment=$idComment; + } + public function getContent(){ + return $this->content; + } + public function getIdOwner(){ + return $this->idOwner; + } + public function getIdPost(){ + return $this->idPost; + } + public function getIdComment(){ + return $this->idComment; + } + public function getTime(){ + return $this->time; + } + +} \ No newline at end of file diff --git a/Tuiter/src/Models/CommentNull.php b/Tuiter/src/Models/CommentNull.php new file mode 100644 index 0000000..ef33caf --- /dev/null +++ b/Tuiter/src/Models/CommentNull.php @@ -0,0 +1,34 @@ +content=""; + $this->idOwner=0; + $this->time=0; + $this->idPost=0; + $this->idComment=0; + } + public function getContent(){ + return $this->content; + } + public function getIdOwner(){ + return $this->idOwner; + } + public function getIdPost(){ + return $this->idPost; + } + public function getIdComment(){ + return $this->idComment; + } + public function getTime(){ + return $this->time; + } + +} \ No newline at end of file diff --git a/Tuiter/src/Services/CommentService.php b/Tuiter/src/Services/CommentService.php new file mode 100644 index 0000000..497a7be --- /dev/null +++ b/Tuiter/src/Services/CommentService.php @@ -0,0 +1,67 @@ +collection = $collection; + } + public function create(string $content, User $user, Post $post){ + $comment=new Comment(time(),$content,$post->getPostId(),$user->getUserId(),md5(microtime())); + $result=$this->collection->insertOne( + array( + "content"=>$comment->getContent(), + "idOwner"=>$comment->getIdOwner(), + "idPost"=>$comment->getIdPost(), + "idComment"=>$comment->getIdComment(), + "time"=>$comment->getTime() + ) + ); + if($result->getInsertedCount()!=1){ + return new CommentNull(time(),$content,$post->getPostId(),$user->getUserId(),md5(microtime())); + } + return $comment; + } + public function getAllCommentsFromPost(Post $post){ + $comments=array(); + $result=$this->collection->find( + array( + 'idPost'=>$post->getPostId() + ) + ); + foreach($result as $info){ + $newComment=new Comment( + $info["time"], + $info["content"], + $info["idPost"], + $info["idOwner"], + $info["idComment"] + ); + $comments[]=$newComment; + } + return $comments; + } + public function getRandomComment(Post $post){ + $comments=array(); + $result=$this->collection->find( + array( + 'idPost'=>$post->getPostId() + ) + ); + $newComment=new Comment( + $result["time"], + $result["content"], + $result["idPost"], + $result["idOwner"], + $result["idComment"] + ); + return $newComment; + } + +} \ No newline at end of file diff --git a/Tuiter/tests/Services/CommentServiceTest.php b/Tuiter/tests/Services/CommentServiceTest.php new file mode 100644 index 0000000..99f07e1 --- /dev/null +++ b/Tuiter/tests/Services/CommentServiceTest.php @@ -0,0 +1,58 @@ +CommentServiceTest->CommentTest; + $collectionUserService = $connection->CommentServiceTest->UserTest; + $collectionPostService = $connection->CommentServiceTest->PostTest; + $collectionPostService->drop(); + $collectionComment->drop(); + $collectionUserService->drop(); + $this->commentService=new CommentService($collectionComment); + $this->userService = new UserService($collectionUserService); + $this->postService= new PostService($collectionPostService); + } + public function testClassExists(){ + $this->assertTrue(class_exists("\Tuiter\Services\CommentService")); + } + public function testComment(){ + $eliel = new User("eliel", "Heber", "1234"); + $edu = new User("edu", "Edward", "1234"); + $this->userService->register("eliel", "Heber", "123456"); + $this->userService->register("edu", "Edward", "123456"); + $post=$this->postService->create('hello man',$edu); + $this->assertTrue($post instanceof Post); + $pudo=$this->commentService->create("boludito",$eliel,$post); + $this->assertEquals($pudo->getContent(),"boludito"); + } + public function testGetCommentsFromPost(){ + $eliel = new User("eliel", "Heber", "1234"); + $edu = new User("edu", "Edward", "1234"); + $this->userService->register("eliel", "Heber", "123456"); + $this->userService->register("edu", "Edward", "123456"); + $post=$this->postService->create('hello man',$edu); + $this->assertTrue($post instanceof Post); + $arrayDeComments=array(); + $comment1=$this->commentService->create("boludito",$eliel,$post); + $arrayDeComments[]=$comment1; + $comment2=$this->commentService->create("pelotudito",$eliel,$post); + $arrayDeComments[]=$comment2; + $comment3=$this->commentService->create("marico",$edu,$post); + $arrayDeComments[]=$comment3; + $comment4=$this->commentService->create("mamahuevo",$eliel,$post); + $arrayDeComments[]=$comment4; + $comment5=$this->commentService->create("ojete",$eliel,$post); + $arrayDeComments[]=$comment5; + $this->assertEquals($this->commentService->getAllCommentsFromPost($post), $arrayDeComments); + + } +} \ No newline at end of file