Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 レシピの取得系APIの修正 & マイレシピの公開非公開フラグの適応 #137

Merged
merged 6 commits into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
import { cookies } from "next/headers";
import { redirect } from "next/navigation";

import { prisma } from "@/src/lib/prisma";
import { ActionsResult } from "@/src/types/ActionsResult";
import { Database } from "@/src/types/SupabaseTypes";
import { createServerActionClient } from "@supabase/auth-helpers-nextjs";

import { prisma } from "../lib/prisma";
import { ActionsResult } from "../types/ActionsResult";
import { Database } from "../types/SupabaseTypes";

export const addCartList = async (recipeId: string, ingredientId: number): Promise<ActionsResult> => {
const supabaseServerClient = createServerActionClient<Database>({ cookies });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import { revalidatePath } from "next/cache";
import { cookies } from "next/headers";
import { redirect } from "next/navigation";

import { prisma } from "@/src/lib/prisma";
import { ActionsResult } from "@/src/types/ActionsResult";
import { Database } from "@/src/types/SupabaseTypes";
import { createServerActionClient } from "@supabase/auth-helpers-nextjs";

import { prisma } from "../lib/prisma";
import { ActionsResult } from "../types/ActionsResult";
import { Database } from "../types/SupabaseTypes";

export const favoriteRecipe = async (recipeId: string): Promise<ActionsResult> => {
const {
data: { session },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import { revalidatePath } from "next/cache";
import { cookies } from "next/headers";
import { redirect } from "next/navigation";

import { prisma } from "@/src/lib/prisma";
import { ActionsResult } from "@/src/types/ActionsResult";
import { Database } from "@/src/types/SupabaseTypes";
import { createServerActionClient } from "@supabase/auth-helpers-nextjs";

import { prisma } from "../lib/prisma";
import { ActionsResult } from "../types/ActionsResult";
import { Database } from "../types/SupabaseTypes";

// シェフをフォローする
export const followChef = async (followedId: string): Promise<ActionsResult> => {
const supabaseServerClient = createServerActionClient<Database>({ cookies });
Expand Down
3 changes: 1 addition & 2 deletions src/actions/deleteChefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import { revalidatePath } from "next/cache";

import { prisma } from "@/src/lib/prisma";

import { ActionsResult } from "../types/ActionsResult";
import { ActionsResult } from "@/src/types/ActionsResult";

export const deleteChefs = async (chefIds: string[]): Promise<ActionsResult> => {
try {
Expand Down
3 changes: 1 addition & 2 deletions src/actions/deleteDraftRecipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import { revalidatePath } from "next/cache";
import { cookies } from "next/headers";

import { prisma } from "@/src/lib/prisma";
import { Database } from "@/src/types/SupabaseTypes";
import { createServerActionClient } from "@supabase/auth-helpers-nextjs";

import { Database } from "../types/SupabaseTypes";

type DeleteDraftRecipeResult = {
isSuccess: boolean;
error?: Error;
Expand Down
4 changes: 2 additions & 2 deletions src/actions/deleteMemo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import { revalidatePath } from "next/cache";

import { prisma } from "../lib/prisma";
import { ActionsResult } from "../types/ActionsResult";
import { prisma } from "@/src/lib/prisma";
import { ActionsResult } from "@/src/types/ActionsResult";

export const deleteMemo = async (id: number): Promise<ActionsResult> => {
try {
Expand Down
5 changes: 2 additions & 3 deletions src/actions/deleteRecipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import { cookies } from "next/headers";
import { redirect } from "next/navigation";

import { prisma } from "@/src/lib/prisma";
import { ActionsResult } from "@/src/types/ActionsResult";
import { Database } from "@/src/types/SupabaseTypes";
import { createServerActionClient } from "@supabase/auth-helpers-nextjs";

import { ActionsResult } from "../types/ActionsResult";
import { Database } from "../types/SupabaseTypes";

export const deleteRecipe = async (id: string): Promise<ActionsResult> => {
const {
data: { session },
Expand Down
5 changes: 2 additions & 3 deletions src/actions/getAuthenticatedUser.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { cookies } from "next/headers";
import { redirect } from "next/navigation";

import { prisma } from "@/src/lib/prisma";
import { Database } from "@/src/types/SupabaseTypes";
import { createServerComponentClient } from "@supabase/auth-helpers-nextjs";

import { prisma } from "../lib/prisma";
import { Database } from "../types/SupabaseTypes";

export const getAuthenticatedUser = async () => {
const supabaseServerClient = createServerComponentClient<Database>({ cookies });

Expand Down
5 changes: 2 additions & 3 deletions src/actions/getCartList.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { cookies } from "next/headers";
import { redirect } from "next/navigation";

import { prisma } from "@/src/lib/prisma";
import { Database } from "@/src/types/SupabaseTypes";
import { createServerComponentClient } from "@supabase/auth-helpers-nextjs";

import { prisma } from "../lib/prisma";
import { Database } from "../types/SupabaseTypes";

export const getCartList = async () => {
const supabaseServerClient = createServerComponentClient<Database>({ cookies });

Expand Down
5 changes: 2 additions & 3 deletions src/actions/getChefById.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { cookies } from "next/headers";
import { notFound, redirect } from "next/navigation";

import { prisma } from "@/src/lib/prisma";
import { Database } from "@/src/types/SupabaseTypes";
import { createServerComponentClient } from "@supabase/auth-helpers-nextjs";

import { prisma } from "../lib/prisma";
import { Database } from "../types/SupabaseTypes";

export const getChefById = async ({ id, orderByLikes = false }: { id: string; orderByLikes?: boolean }) => {
const chef = await prisma.user.findUnique({
where: {
Expand Down
2 changes: 1 addition & 1 deletion src/actions/getChefs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { prisma } from "../lib/prisma";
import { prisma } from "@/src/lib/prisma";

export const getChefs = async () => {
const chefs = await prisma.user.findMany({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createServerComponentClient } from "@supabase/auth-helpers-nextjs";
import { prisma } from "../lib/prisma";
import { Database } from "../types/SupabaseTypes";

export const getFollowingChefs = async () => {
export const getChefsInMyFollowingList = async () => {
const supabaseServerClient = createServerComponentClient<Database>({ cookies });

const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { addDays } from "date-fns";

import { prisma } from "../lib/prisma";

export const getTopFollowersInLast3Days = async () => {
export const getChefsWithTopFollowersInLast3Days = async () => {
const threeDaysAgo = addDays(new Date(), -3);

const followers = await prisma.userFollower.groupBy({
Expand Down
2 changes: 1 addition & 1 deletion src/actions/getDraftRecipe.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { prisma } from "../lib/prisma";
import { prisma } from "@/src/lib/prisma";

export const getDraftRecipe = async (id: string) => {
const draftRecipe = await prisma.draftRecipe.findUnique({
Expand Down
5 changes: 2 additions & 3 deletions src/actions/getDraftRecipes.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { cookies } from "next/headers";
import { redirect } from "next/navigation";

import { prisma } from "@/src/lib/prisma";
import { Database } from "@/src/types/SupabaseTypes";
import { createServerComponentClient } from "@supabase/auth-helpers-nextjs";

import { prisma } from "../lib/prisma";
import { Database } from "../types/SupabaseTypes";

export const getDraftRecipes = async () => {
const supabaseServerClient = createServerComponentClient<Database>({ cookies });

Expand Down
5 changes: 2 additions & 3 deletions src/actions/getMemos.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { cookies } from "next/headers";
import { redirect } from "next/navigation";

import { prisma } from "@/src/lib/prisma";
import { Database } from "@/src/types/SupabaseTypes";
import { createServerComponentClient } from "@supabase/auth-helpers-nextjs";

import { prisma } from "../lib/prisma";
import { Database } from "../types/SupabaseTypes";

export const getMemos = async () => {
const supabaseServerClient = createServerComponentClient<Database>({ cookies });

Expand Down
8 changes: 3 additions & 5 deletions src/actions/getMyRecipes.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { cookies } from "next/headers";
import { redirect } from "next/navigation";

import { prisma } from "@/src/lib/prisma";
import { PaginationParams } from "@/src/types/PaginationParams";
import { Database } from "@/src/types/SupabaseTypes";
import { createServerComponentClient } from "@supabase/auth-helpers-nextjs";

import { kInfiniteScrollCount } from "../constants/constants";
import { prisma } from "../lib/prisma";
import { PaginationParams } from "../types/PaginationParams";
import { Database } from "../types/SupabaseTypes";

export const getMyRecipes = async (
{ orderByLikes, limit, skip }: { orderByLikes?: boolean } & PaginationParams = {
orderByLikes: false,
Expand Down
5 changes: 2 additions & 3 deletions src/actions/getRecipeById.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { cookies } from "next/headers";
import { notFound, redirect } from "next/navigation";

import { prisma } from "@/src/lib/prisma";
import { Database } from "@/src/types/SupabaseTypes";
import { createServerComponentClient } from "@supabase/auth-helpers-nextjs";

import { prisma } from "../lib/prisma";
import { Database } from "../types/SupabaseTypes";

export const getRecipeById = async (id: string) => {
const supabaseServerClient = createServerComponentClient<Database>({ cookies });

Expand Down
11 changes: 8 additions & 3 deletions src/actions/getRecipes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { kInfiniteScrollCount } from "../constants/constants";
import { prisma } from "../lib/prisma";
import { PaginationParams } from "../types/PaginationParams";
import { kInfiniteScrollCount } from "@/src/constants/constants";
import { prisma } from "@/src/lib/prisma";
import { PaginationParams } from "@/src/types/PaginationParams";

export const getRecipes = async (
{ skip, limit }: PaginationParams = {
Expand All @@ -16,6 +16,11 @@ export const getRecipes = async (
orderBy: {
createdAt: "desc",
},
where: {
user: {
role: "CHEF",
},
},
skip,
take: limit,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { cookies } from "next/headers";
import { redirect } from "next/navigation";

import { kInfiniteScrollCount } from "@/src/constants/constants";
import { prisma } from "@/src/lib/prisma";
import { PaginationParams } from "@/src/types/PaginationParams";
import { Database } from "@/src/types/SupabaseTypes";
import { createServerComponentClient } from "@supabase/auth-helpers-nextjs";

import { kInfiniteScrollCount } from "../constants/constants";
import { prisma } from "../lib/prisma";
import { PaginationParams } from "../types/PaginationParams";
import { Database } from "../types/SupabaseTypes";

export const getMyFavoriteRecipes = async (
export const getRecipesInMyFavorites = async (
{ skip, limit }: PaginationParams = {
skip: 0,
limit: kInfiniteScrollCount,
Expand All @@ -25,6 +24,10 @@ export const getMyFavoriteRecipes = async (
const favoriteRecipes = await prisma.favorite.findMany({
where: {
userId: session.user.id,
recipe: {
// マイレシピは非公開のものも含めて取得する
OR: [{ isPublished: true }, { userId: session.user.id }],
},
},
include: {
recipe: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { cookies } from "next/headers";
import { redirect } from "next/navigation";

import { kInfiniteScrollCount } from "@/src/constants/constants";
import { prisma } from "@/src/lib/prisma";
import { PaginationParams } from "@/src/types/PaginationParams";
import { Database } from "@/src/types/SupabaseTypes";
import { createServerComponentClient } from "@supabase/auth-helpers-nextjs";

import { kInfiniteScrollCount } from "../constants/constants";
import { prisma } from "../lib/prisma";
import { PaginationParams } from "../types/PaginationParams";
import { Database } from "../types/SupabaseTypes";

export const getNewRecipesFromFollowingChefs = async (
export const getRecipesNewFromFollowedChefs = async (
{ limit, skip }: PaginationParams = {
skip: 0,
limit: kInfiniteScrollCount,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { kInfiniteScrollCount } from "@/src/constants/constants";
import { prisma } from "@/src/lib/prisma";
import { PaginationParams } from "@/src/types/PaginationParams";
import { addDays } from "date-fns";

import { kInfiniteScrollCount } from "../constants/constants";
import { prisma } from "../lib/prisma";
import { PaginationParams } from "../types/PaginationParams";

export const getTopFavoriteRecipesInLast3Days = async (
export const getRecipesTopFavoritesInLast3Days = async (
{ limit, skip }: PaginationParams = {
skip: 0,
limit: kInfiniteScrollCount,
Expand Down Expand Up @@ -38,6 +37,9 @@ export const getTopFavoriteRecipesInLast3Days = async (
id: {
in: recipeIds,
},
user: {
role: "CHEF",
},
},
include: {
RecipeImage: true,
Expand Down
58 changes: 58 additions & 0 deletions src/actions/patchRecipePublishStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"use server";

import { revalidatePath } from "next/cache";

import { prisma } from "@/src/lib/prisma";
import { ActionsResult } from "@/src/types/ActionsResult";

export const patchRecipePublishStatus = async (id: string): Promise<ActionsResult> => {
try {
const currentRecipe = await prisma.recipe.findUnique({
where: {
id,
},
select: {
isPublished: true,
},
});

if (!currentRecipe) {
return {
isSuccess: false,
error: "レシピの取得に失敗しました🥲",
};
}

const updatedRecipe = await prisma.recipe.update({
where: {
id,
},
data: {
isPublished: !currentRecipe.isPublished,
},
select: {
isPublished: true,
},
});

if (!updatedRecipe) {
return {
isSuccess: false,
error: "レシピの更新に失敗しました🥲",
};
}

revalidatePath(`/my-recipe/${id}`);

return {
isSuccess: true,
message: `レシピの公開状態を${updatedRecipe.isPublished ? "「公開」" : "「非公開」"}に変更しました🎉`,
};
} catch (error) {
console.log(error);
return {
isSuccess: false,
error: "レシピの公開状態の変更に失敗しました🥲",
};
}
};
7 changes: 3 additions & 4 deletions src/actions/postAllToCart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import { revalidatePath } from "next/cache";
import { cookies } from "next/headers";
import { redirect } from "next/navigation";

import { prisma } from "@/src/lib/prisma";
import { ActionsResult } from "@/src/types/ActionsResult";
import { Database } from "@/src/types/SupabaseTypes";
import { createServerActionClient } from "@supabase/auth-helpers-nextjs";

import { prisma } from "../lib/prisma";
import { ActionsResult } from "../types/ActionsResult";
import { Database } from "../types/SupabaseTypes";

export const postAllToCart = async (recipeId: string, ingredientIds: number[]): Promise<ActionsResult> => {
const supabaseServerClient = createServerActionClient<Database>({ cookies });

Expand Down
Loading