From 962ed95c6fc70e1ae27c29de5ac39b7610d2b3bb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kratochv=C3=ADl?= <xkrato10@fi.muni.cz>
Date: Wed, 15 Jun 2022 21:27:24 +0200
Subject: [PATCH] feat: implemented categories CRUD

---
 .../src/api/categories/categories.module.ts   |  3 +-
 .../src/api/categories/categories.service.ts  | 33 ++++++++++++++++---
 2 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/backend/src/api/categories/categories.module.ts b/backend/src/api/categories/categories.module.ts
index 8776b44..6590c8b 100644
--- a/backend/src/api/categories/categories.module.ts
+++ b/backend/src/api/categories/categories.module.ts
@@ -1,9 +1,10 @@
 import { Module } from '@nestjs/common';
 import { CategoriesService } from './categories.service';
 import { CategoriesController } from './categories.controller';
+import { PrismaService } from 'src/prisma.service';
 
 @Module({
   controllers: [CategoriesController],
-  providers: [CategoriesService]
+  providers: [CategoriesService, PrismaService]
 })
 export class CategoriesModule {}
diff --git a/backend/src/api/categories/categories.service.ts b/backend/src/api/categories/categories.service.ts
index 1ee0421..71eebc7 100644
--- a/backend/src/api/categories/categories.service.ts
+++ b/backend/src/api/categories/categories.service.ts
@@ -1,26 +1,49 @@
 import { Injectable } from '@nestjs/common';
+import { PrismaService } from 'src/prisma.service';
 import { CreateCategoryDto } from './dto/create-category.dto';
 import { UpdateCategoryDto } from './dto/update-category.dto';
 
 @Injectable()
 export class CategoriesService {
+  constructor(private prisma: PrismaService) {}
+
   create(createCategoryDto: CreateCategoryDto) {
-    return 'This action adds a new category';
+    return this.prisma.category.create({
+      data: createCategoryDto
+    });
   }
 
   findAll() {
-    return `This action returns all categories`;
+    return this.prisma.category.findMany();
   }
 
   findOne(id: string) {
-    return `This action returns a #${id} category`;
+    return this.prisma.category.findUnique({ 
+      where: {
+        id
+      }
+    });
   }
 
   update(id: string, updateCategoryDto: UpdateCategoryDto) {
-    return `This action updates a #${id} category`;
+    return this.prisma.category.update({
+      where: {
+        id
+      },
+      data: {
+        ...updateCategoryDto
+      }
+    });
   }
 
   remove(id: string) {
-    return `This action removes a #${id} category`;
+    return this.prisma.category.update({
+      where: {
+        id
+      },
+      data: {
+        deletedAt: new Date()
+      }
+    });
   }
 }
-- 
GitLab