From af9fe70ec6dcb0c0770ee425f0655d8b40e8cf6e Mon Sep 17 00:00:00 2001 From: babak alizadeh Date: Thu, 23 Feb 2023 08:15:56 -0500 Subject: [PATCH] some progress --- config/packages/nelmio_cors.yaml | 12 +- public/index.php | 1 - src/Controller/BlogController.php | 113 +++++++++++++++- src/Controller/BusinessController.php | 42 ++++++ src/Entity/BlogCat.php | 40 ++++++ src/Entity/BlogPost.php | 158 ++++++++++++++++++++++ src/Entity/Business.php | 30 ++++ src/Entity/User.php | 34 +++++ src/Kernel.php | 10 ++ src/Repository/BlogPostRepository.php | 88 ++++++++++++ src/Repository/StackContentRepository.php | 18 ++- 11 files changed, 531 insertions(+), 15 deletions(-) create mode 100644 src/Controller/BusinessController.php create mode 100644 src/Entity/BlogPost.php create mode 100644 src/Repository/BlogPostRepository.php diff --git a/config/packages/nelmio_cors.yaml b/config/packages/nelmio_cors.yaml index c766508..2efc2db 100644 --- a/config/packages/nelmio_cors.yaml +++ b/config/packages/nelmio_cors.yaml @@ -1,10 +1,14 @@ nelmio_cors: defaults: origin_regex: true - allow_origin: ['%env(CORS_ALLOW_ORIGIN)%'] - allow_methods: ['GET', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE'] - allow_headers: ['Content-Type', 'Authorization'] + allow_origin: ['*'] + allow_methods: ['*'] + allow_headers: ['*'] expose_headers: ['Link'] max_age: 3600 paths: - '^/': null + '^/api/': + allow_origin: [ '*' ] + allow_headers: [ '*' ] + allow_methods: [ 'POST', 'PUT', 'GET', 'DELETE' ] + max_age: 3600 diff --git a/public/index.php b/public/index.php index 4c1ee8a..bb18414 100644 --- a/public/index.php +++ b/public/index.php @@ -1,7 +1,6 @@ getRepository(BlogCat::class)->findAll(); + foreach ($cats as $cat){ + $temp = []; + $temp['id'] = $cat->getId(); + $temp['name'] = $cat->getLabel(); + $temp['code'] = $cat->getCode(); + $result[] = $temp; + } + return $this->json($result); + } + + #[Route('/api/blog/insert', name: 'app_blog_content_insert')] + public function app_blog_content_insert(Request $request,SerializerInterface $serializer, EntityManagerInterface $entityManager): JsonResponse + { + $params = []; + if ($content = $request->getContent()) { + $params = json_decode($content, true); + } + if(array_key_exists('intro',$params) && array_key_exists('title',$params) && array_key_exists('cat',$params) && array_key_exists('body',$params )){ + $cat = $entityManager->getRepository(BlogCat::class)->find($params['cat']); + $post = new BlogPost(); + $post->setBody($params['body']); + $post->setCat($cat); + $post->setIntero($params['intro']); + $post->setImg($params['img']); + $post->setTitle($params['title']); + $post->setDateSubmit(time()); + $post->setViews(1); + $post->setSubmitter($this->getUser()); + $post->setUrl(str_replace(' ','_',$params['title'])); + $entityManager->persist($post); + $entityManager->flush(); + return $this->json([ + 'error'=> 0, + 'message'=> 'ok', + 'url'=>$post->getUrl() + ]); + } return $this->json([ - 'error' => 0, - 'data' => $serializer->serialize($entityManager->getRepository(BlogCat::class)->findAll(),'json'), + 'error'=> 999, + 'message'=> 'تمام موارد لازم را وارد کنید.' ]); } + + /** + * @throws \Doctrine\ORM\NonUniqueResultException + * @throws \Doctrine\ORM\NoResultException + */ + #[Route('/api/blog/contents/search', name: 'app_blog_contents_get')] + public function app_blog_contents_get(Jdate $jdate, Provider $provider,Request $request,SerializerInterface $serializer, EntityManagerInterface $entityManager): JsonResponse + { + $params = []; + if ($content = $request->getContent()) { + $params = json_decode($content, true); + } + $params = $provider->createSearchParams($request); + $items = $entityManager->getRepository(BlogPost::class)->search($params); + $response = []; + foreach ($items as $item){ + $temp = []; + $temp['id'] = $item->getId(); + $temp['title'] = $item->getTitle(); + $temp['intero'] = $item->getIntero(); + $temp['body'] = $item->getBody(); + $temp['submitter'] = $item->getSubmitter()->getFullName(); + $temp['dateSubmit'] = $jdate->pastTime($item->getDateSubmit()); + $temp['cat'] = $item->getCat()->getLabel(); + $temp['views'] = $item->getViews(); + $temp['url'] = $item->getUrl(); + $temp['img'] = $item->getImg(); + $response[] = $temp; + } + // calc has next page + $nextPage = true; + if((int)$provider->maxPages($params,$entityManager->getRepository(BlogPost::class)->getAllContentCount()) == $params['page']) + $nextPage = false; + return $this->json([ + 'data'=>$response, + 'nextPage'=>$nextPage + ]); + } + + /** + * @throws \Doctrine\ORM\NonUniqueResultException + * @throws \Doctrine\ORM\NoResultException + */ + #[Route('/api/blog/post/get/{url}', name: 'app_blog_post_get')] + public function app_blog_post_get($url,Jdate $jdate, Provider $provider,Request $request,SerializerInterface $serializer, EntityManagerInterface $entityManager): JsonResponse + { + $post = $entityManager->getRepository(BlogPost::class)->findOneBy(['url'=>$url]); + if(!$post) + throw $this->createNotFoundException(); + $temp = []; + $temp['id'] = $post->getId(); + $temp['title'] = $post->getTitle(); + $temp['intero'] = $post->getIntero(); + $temp['body'] = $post->getBody(); + $temp['submitter'] = $post->getSubmitter()->getFullName(); + $temp['dateSubmit'] = $jdate->pastTime($post->getDateSubmit()); + $temp['cat'] = $post->getCat()->getLabel(); + $temp['views'] = $post->getViews(); + $temp['url'] = $post->getUrl(); + $temp['img'] = $post->getImg(); + return $this->json($temp); + } } diff --git a/src/Controller/BusinessController.php b/src/Controller/BusinessController.php new file mode 100644 index 0000000..00ac2c7 --- /dev/null +++ b/src/Controller/BusinessController.php @@ -0,0 +1,42 @@ +getRepository(Business::class)->findBy(['owner'=>$user]); + $response = []; + foreach ($buss as $bus){ + $temp = []; + $temp['id'] = $bus->getId(); + $temp['name'] = $bus->getName(); + $temp['owner'] = $bus->getOwner()->getFullName(); + $temp['legal_name'] = $bus->getLegalName(); + $response[] = $temp; + } + + return $this->json($response); + } + + #[Route('/api/business/list/count', name: 'api_bussiness_list_count')] + public function api_bussiness_list_count(#[CurrentUser] ?User $user,EntityManagerInterface $entityManager): Response + { + $buss = $entityManager->getRepository(Business::class)->findBy(['owner'=>$user]); + $response = ['count'=>count($buss)]; + return $this->json($response); + } +} diff --git a/src/Entity/BlogCat.php b/src/Entity/BlogCat.php index f465e2b..b6551c2 100644 --- a/src/Entity/BlogCat.php +++ b/src/Entity/BlogCat.php @@ -3,6 +3,8 @@ namespace App\Entity; use App\Repository\BlogCatRepository; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: BlogCatRepository::class)] @@ -19,6 +21,14 @@ class BlogCat #[ORM\Column(length: 255)] private ?string $code = null; + #[ORM\OneToMany(mappedBy: 'cat', targetEntity: BlogPost::class, orphanRemoval: true)] + private Collection $blogPosts; + + public function __construct() + { + $this->blogPosts = new ArrayCollection(); + } + public function getId(): ?int { return $this->id; @@ -47,4 +57,34 @@ class BlogCat return $this; } + + /** + * @return Collection + */ + public function getBlogPosts(): Collection + { + return $this->blogPosts; + } + + public function addBlogPost(BlogPost $blogPost): self + { + if (!$this->blogPosts->contains($blogPost)) { + $this->blogPosts->add($blogPost); + $blogPost->setCat($this); + } + + return $this; + } + + public function removeBlogPost(BlogPost $blogPost): self + { + if ($this->blogPosts->removeElement($blogPost)) { + // set the owning side to null (unless already changed) + if ($blogPost->getCat() === $this) { + $blogPost->setCat(null); + } + } + + return $this; + } } diff --git a/src/Entity/BlogPost.php b/src/Entity/BlogPost.php new file mode 100644 index 0000000..017384e --- /dev/null +++ b/src/Entity/BlogPost.php @@ -0,0 +1,158 @@ +id; + } + + public function getTitle(): ?string + { + return $this->title; + } + + public function setTitle(string $title): self + { + $this->title = $title; + + return $this; + } + + public function getBody(): ?string + { + return $this->body; + } + + public function setBody(string $body): self + { + $this->body = $body; + + return $this; + } + + public function getSubmitter(): ?User + { + return $this->submitter; + } + + public function setSubmitter(?User $submitter): self + { + $this->submitter = $submitter; + + return $this; + } + + public function getDateSubmit(): ?string + { + return $this->dateSubmit; + } + + public function setDateSubmit(string $dateSubmit): self + { + $this->dateSubmit = $dateSubmit; + + return $this; + } + + public function getViews(): ?string + { + return $this->views; + } + + public function setViews(string $views): self + { + $this->views = $views; + + return $this; + } + + public function getImg(): ?string + { + return $this->img; + } + + public function setImg(?string $img): self + { + $this->img = $img; + + return $this; + } + + public function getCat(): ?BlogCat + { + return $this->cat; + } + + public function setCat(?BlogCat $cat): self + { + $this->cat = $cat; + + return $this; + } + + public function getUrl(): ?string + { + return $this->url; + } + + public function setUrl(string $url): self + { + $this->url = $url; + + return $this; + } + + public function getIntero(): ?string + { + return $this->intero; + } + + public function setIntero(string $intero): self + { + $this->intero = $intero; + + return $this; + } +} diff --git a/src/Entity/Business.php b/src/Entity/Business.php index 69655ea..c246b9a 100644 --- a/src/Entity/Business.php +++ b/src/Entity/Business.php @@ -17,6 +17,12 @@ class Business #[ORM\JoinColumn(nullable: false)] private ?User $owner = null; + #[ORM\Column(length: 255)] + private ?string $name = null; + + #[ORM\Column(length: 255)] + private ?string $legalName = null; + public function getId(): ?int { return $this->id; @@ -33,4 +39,28 @@ class Business return $this; } + + public function getName(): ?string + { + return $this->name; + } + + public function setName(string $name): self + { + $this->name = $name; + + return $this; + } + + public function getLegalName(): ?string + { + return $this->legalName; + } + + public function setLegalName(string $legalName): self + { + $this->legalName = $legalName; + + return $this; + } } diff --git a/src/Entity/User.php b/src/Entity/User.php index c78ba1c..b583bcc 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -47,12 +47,16 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface #[ORM\OneToMany(mappedBy: 'submitter', targetEntity: StackContent::class, orphanRemoval: true)] private Collection $stackContents; + #[ORM\OneToMany(mappedBy: 'submitter', targetEntity: BlogPost::class, orphanRemoval: true)] + private Collection $blogPosts; + public function __construct() { $this->userTokens = new ArrayCollection(); $this->businesses = new ArrayCollection(); $this->guideContents = new ArrayCollection(); $this->stackContents = new ArrayCollection(); + $this->blogPosts = new ArrayCollection(); } public function getId(): ?int @@ -268,4 +272,34 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface return $this; } + + /** + * @return Collection + */ + public function getBlogPosts(): Collection + { + return $this->blogPosts; + } + + public function addBlogPost(BlogPost $blogPost): self + { + if (!$this->blogPosts->contains($blogPost)) { + $this->blogPosts->add($blogPost); + $blogPost->setSubmitter($this); + } + + return $this; + } + + public function removeBlogPost(BlogPost $blogPost): self + { + if ($this->blogPosts->removeElement($blogPost)) { + // set the owning side to null (unless already changed) + if ($blogPost->getSubmitter() === $this) { + $blogPost->setSubmitter(null); + } + } + + return $this; + } } diff --git a/src/Kernel.php b/src/Kernel.php index 779cd1f..80cb5d3 100644 --- a/src/Kernel.php +++ b/src/Kernel.php @@ -8,4 +8,14 @@ use Symfony\Component\HttpKernel\Kernel as BaseKernel; class Kernel extends BaseKernel { use MicroKernelTrait; + + public function getCacheDir(): string + { + return str_replace('core','',dirname(__DIR__)).'cache/'.$this->environment.'/cache'; + } + + public function getLogDir(): string + { + return str_replace('core','',dirname(__DIR__)).'logs/'.$this->environment.'/log'; + } } diff --git a/src/Repository/BlogPostRepository.php b/src/Repository/BlogPostRepository.php new file mode 100644 index 0000000..6bbf2c0 --- /dev/null +++ b/src/Repository/BlogPostRepository.php @@ -0,0 +1,88 @@ + + * + * @method BlogPost|null find($id, $lockMode = null, $lockVersion = null) + * @method BlogPost|null findOneBy(array $criteria, array $orderBy = null) + * @method BlogPost[] findAll() + * @method BlogPost[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) + */ +class BlogPostRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, BlogPost::class); + } + + public function save(BlogPost $entity, bool $flush = false): void + { + $this->getEntityManager()->persist($entity); + + if ($flush) { + $this->getEntityManager()->flush(); + } + } + + public function remove(BlogPost $entity, bool $flush = false): void + { + $this->getEntityManager()->remove($entity); + + if ($flush) { + $this->getEntityManager()->flush(); + } + } + + /* + * @return StackContent[] + */ + public function search($params): array + { + $result = $this->createQueryBuilder('s') + ->setMaxResults($params['count']) + ->setFirstResult(($params['page'] -1) * $params['count']); + $result->orderBy('s.id', 'DESC'); + return $result->getQuery()->getResult(); + } + /** + * @throws \Doctrine\ORM\NonUniqueResultException + * @throws \Doctrine\ORM\NoResultException + */ + public function getAllContentCount(): int{ + return $this->createQueryBuilder('s') + ->select('count(s.id)') + ->getQuery() + ->getSingleScalarResult() + ; + } +// /** +// * @return BlogPost[] Returns an array of BlogPost objects +// */ +// public function findByExampleField($value): array +// { +// return $this->createQueryBuilder('b') +// ->andWhere('b.exampleField = :val') +// ->setParameter('val', $value) +// ->orderBy('b.id', 'ASC') +// ->setMaxResults(10) +// ->getQuery() +// ->getResult() +// ; +// } + +// public function findOneBySomeField($value): ?BlogPost +// { +// return $this->createQueryBuilder('b') +// ->andWhere('b.exampleField = :val') +// ->setParameter('val', $value) +// ->getQuery() +// ->getOneOrNullResult() +// ; +// } +} diff --git a/src/Repository/StackContentRepository.php b/src/Repository/StackContentRepository.php index 965f616..040eba8 100644 --- a/src/Repository/StackContentRepository.php +++ b/src/Repository/StackContentRepository.php @@ -44,16 +44,20 @@ class StackContentRepository extends ServiceEntityRepository */ public function search($params): array { - return $this->createQueryBuilder('s') + $result = $this->createQueryBuilder('s') ->setMaxResults($params['count']) ->setFirstResult(($params['page'] -1) * $params['count']) ->orWhere('s.body LIKE :key') - ->andWhere('s.upper is NULL') - ->orderBy('s.id', 'DESC') - ->setParameter('key','%' . $params['key'] . '%') - ->getQuery() - ->getResult() - ; + ->andWhere('s.upper is NULL'); + if (array_key_exists('cat',$params)){ + if($params['cat'] != 'non') + $result->andWhere('s.cat = :cat') + ->setParameter('cat',$params['cat']); + } + $result->orderBy('s.id', 'DESC') + ->setParameter('key','%' . $params['key'] . '%'); + + return $result->getQuery()->getResult(); } /**