Skip to content

Commit

Permalink
#9 feat: 레시피글 삭제 API
Browse files Browse the repository at this point in the history
  • Loading branch information
JoongHyun-Kim committed Feb 6, 2024
1 parent 1d57aaf commit 0626514
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public enum BaseResponseStatus {

// recipe(2200-2299)
INVALID_RECIPE_IDX(false, 2200, "잘못된 레시피글 idx 입니다."),
NO_RECIPE_WRITER(false, 2201, "해당 레시피글의 작성자가 아닙니다."),
ALREADY_DELETED_RECIPE(false, 2202, "이미 삭제된 레시피글입니다."),

// ingredient(2300-2399)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.List;

import static com.kkobugi.puremarket.common.constants.Constant.ACTIVE;
import static com.kkobugi.puremarket.common.constants.Constant.INACTIVE;
import static com.kkobugi.puremarket.common.enums.BaseResponseStatus.*;
import static com.kkobugi.puremarket.common.enums.IngredientType.INGREDIENT;
import static com.kkobugi.puremarket.common.enums.IngredientType.SAUCE;
Expand Down Expand Up @@ -129,4 +130,26 @@ public void postRecipe(MultipartFile image, RecipePostRequest recipePostRequest)
throw new BaseException(DATABASE_ERROR);
}
}

// 레시피글 삭제
public void deleteRecipe(Long recipeIdx) throws BaseException {
try {
User user = userRepository.findByUserIdx(authService.getUserIdxFromToken()).orElseThrow(() -> new BaseException(INVALID_USER_IDX));
Recipe recipe = recipeRepository.findById(recipeIdx).orElseThrow(() -> new BaseException(INVALID_RECIPE_IDX));

validateWriter(user, recipe);

recipe.delete();
recipeRepository.save(recipe);
} catch (BaseException e) {
throw e;
} catch (Exception e) {
throw new BaseException(DATABASE_ERROR);
}
}

private static void validateWriter(User user, Recipe recipe) throws BaseException {
if (!recipe.getUser().equals(user)) throw new BaseException(NO_RECIPE_WRITER);
if (recipe.getStatus().equals(INACTIVE)) throw new BaseException(ALREADY_DELETED_RECIPE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import lombok.NoArgsConstructor;
import org.hibernate.annotations.DynamicInsert;

import static com.kkobugi.puremarket.common.constants.Constant.INACTIVE;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand Down Expand Up @@ -38,4 +40,8 @@ public Recipe(User user, String title, String content, String recipeImage) {
this.content = content;
this.recipeImage = recipeImage;
}

public void delete() {
this.setStatus(INACTIVE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,15 @@ public BaseResponse<?> postRecipe(@RequestPart(value = "image", required = false
return new BaseResponse<>(e.getStatus());
}
}

// [작성자] 레시피글 삭제
@PatchMapping("/{recipeIdx}")
public BaseResponse<?> deleteRecipe(@PathVariable Long recipeIdx) {
try {
recipeService.deleteRecipe(recipeIdx);
return new BaseResponse<>(SUCCESS);
} catch (BaseException e) {
return new BaseResponse<>(e.getStatus());
}
}
}

0 comments on commit 0626514

Please sign in to comment.