Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
Katsukiniwa committed Feb 28, 2024
1 parent 944eee5 commit 173bd49
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 4 deletions.
32 changes: 32 additions & 0 deletions admin/src/app/(pages)/categories/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { QuestionCard } from "@/components/QuestionCard";
import { getCategoryById, getQuestionsByCategoryId } from "@/lib/data";
import React from "react";

export default async function CategoryDetail({
params,
}: {
params: { id: string };
}) {
const category = await getCategoryById(Number(params.id));
const questions = await getQuestionsByCategoryId(Number(params.id));

return (
<main>
<div className="px-4 md:px-8 py-4 bg-gray-100">
<h2 className="my-4 pl-3 text-xl font-bold border-l-4 border-green-300">
{category.name}の質問を探す
</h2>
<div className="grid xl:grid-cols-4 sm:grid-cols-2 gap-4 mx-auto">
{questions.map((item) => (
<QuestionCard
key={item.id}
id={item.id.toString()}
title={item.title}
content={item.content}
/>
))}
</div>
</div>
</main>
);
}
File renamed without changes.
File renamed without changes.
37 changes: 35 additions & 2 deletions admin/src/lib/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,25 @@ export const getCategories = async () => {
name: string,
name_en: string,
icon: string,
created_at: Date,
updated_at: Date,
}[]
}

export const getCategoryById = async (id: number) => {
const res = await fetch(`http://localhost:3000/categories/${id}`, {
method: "GET",
headers: {
'Accept': 'application/json'
}
}).then(res => res.json())

return res as {
id: number,
name: string,
name_en: string,
icon: string,
}
}

export const getQuestions = async () => {
const res = await fetch('http://localhost:3000/questions', {
method: 'GET',
Expand Down Expand Up @@ -49,3 +63,22 @@ export const getQuestionById = async (id: number) => {
postedByMe: boolean
}
}

export const getQuestionsByCategoryId = async (id: number) => {
const res = await fetch(`http://localhost:3000/categories/${id}/questions`, {
method: 'GET',
headers: {
'Accept': 'application/json'
}
}).then(res => res.json())

// FIXME
res.postedByMe = false

return res as {
id: number,
title: string,
content: string
postedByMe: boolean
}[]
}
1 change: 1 addition & 0 deletions backend/app/controllers/questions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class QuestionsController < ApplicationController
# GET /questions
def index
@questions = Question.all
@questions = Question.where(category_id: params['category_id']) if params['category_id'].present?

render json: @questions
end
Expand Down
1 change: 1 addition & 0 deletions backend/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ services:
- redis
expose: ["3000"]
ports: ["3000:3000"]
command: /bin/sh -c "bin/rails server -b 0.0.0.0"
user: root
working_dir: /app

Expand Down
6 changes: 4 additions & 2 deletions backend/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
Rails.application.routes.draw do
if Rails.env.development?
mount GraphiQL::Rails::Engine, at: '/graphiql', graphql_path: '/graphql'
end
end
post "/graphql", to: "graphql#execute"

root 'top#index'

resources :favorite_questions
resources :draft_questions
resources :tags
resources :categories
resources :categories do
resources :questions, only: [:index]
end
resources :users
resources :questions

Expand Down

0 comments on commit 173bd49

Please sign in to comment.