| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- import {
- Category
- } from "../../../models/Category";
- import {
- UpdateCategoryInput,
- UpdateCategoryResult
- } from "./types";
- const updateCategory = async (userID: string, input: UpdateCategoryInput): Promise<UpdateCategoryResult> => {
- try {
- const {
- categoryID,
- ...updateFields
- } = input;
- const category = await Category.findOne({
- _id: categoryID,
- userID
- });
- if (!category) {
- return {
- message: "category-not-found",
- code: 404
- };
- }
- const updatedCategory = await Category.findByIdAndUpdate(
- categoryID,
- updateFields,
- {
- new: true //güncelledikten sonra döndürmesi için
- }
- );
-
- if (!updatedCategory){
- return {
- message: "category-not-found",
- code: 404
- };
- }
- return {
- message: "category-updated",
- code: 200,
- payload: {
- category: {
- _id: updatedCategory._id.toString(),
- title: updatedCategory.title,
- index: updatedCategory.index
- }
- }
- };
- } catch (error) {
- console.error("UpdateCategory error:", error);
- return {
- message: "internal-server-error",
- code: 500
- };
- }
- };
- export default updateCategory;
|