index.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import {
  2. Category
  3. } from "../../../models/Category";
  4. import {
  5. UpdateCategoryInput,
  6. UpdateCategoryResult
  7. } from "./types";
  8. const updateCategory = async (userID: string, input: UpdateCategoryInput): Promise<UpdateCategoryResult> => {
  9. try {
  10. const {
  11. categoryID,
  12. ...updateFields
  13. } = input;
  14. const category = await Category.findOne({
  15. _id: categoryID,
  16. userID
  17. });
  18. if (!category) {
  19. return {
  20. message: "category-not-found",
  21. code: 404
  22. };
  23. }
  24. const updatedCategory = await Category.findByIdAndUpdate(
  25. categoryID,
  26. updateFields,
  27. {
  28. new: true //güncelledikten sonra döndürmesi için
  29. }
  30. );
  31. if (!updatedCategory){
  32. return {
  33. message: "category-not-found",
  34. code: 404
  35. };
  36. }
  37. return {
  38. message: "category-updated",
  39. code: 200,
  40. payload: {
  41. category: {
  42. _id: updatedCategory._id.toString(),
  43. title: updatedCategory.title,
  44. index: updatedCategory.index
  45. }
  46. }
  47. };
  48. } catch (error) {
  49. console.error("UpdateCategory error:", error);
  50. return {
  51. message: "internal-server-error",
  52. code: 500
  53. };
  54. }
  55. };
  56. export default updateCategory;