Giter Club home page Giter Club logo

qaapi-pbl's Introduction

QAapi-PBL

QAapp-PBLで使うためのAPIサーバーのプログラムです。

クローンからAPIサーバー起動まで

Step 1: リポジトリをクローン

下記のコマンドを実行してください。

# リポジトリをクローンする
git clone https://github.com/itc-s22020/QAapi-PBL.git
# ディレクトリに移動
cd QAapi-PBL

IntelliJ IDEAとかWebstormを使う場合は、プロジェクトを選ぶ画面の右上にある「VCSから取得」みたいなボタンからクローンすればOKです。

Step 2: 依存関係のインストール

下記のコマンドを実行してください。

npm i

Step 3: 設定ファイルを記述

package.jsonがあるディレクトリに.envファイルを作成してください。

作成したら下記の内容を貼り付け、環境に合わせて書き換えてください。

DATABASE_URL="postgresql://postgres:pass@localhost:5432/postgres?schema=public"
SECRET="推測されないランダムな文字列"

Step 4: DBサーバー環境構築

下記のコマンドを実行してください。

# dockerディレクトリに移動
cd docker
# コンテナを起動する
docker compose up -d

docker/docker-compose.ymlの設定は次のようになっています。

項目 内容
データベース PostgreSQL
ユーザー名 postgres
パスワード pass
ポート 5432

Step 5: データベースのマイグレーション

prisma/schema.prismaに書いてあるデータベースの設定をDBサーバーに反映させて、prisma/seed.jsを実行して初期データを作成します。

下記のコマンドを実行してください。

npx prisma migrate dev

実行後に何か出ますが、何も入れずにEnterでOKです。

APIの仕様

次のマークがあるものは認証(アクセストークン)が必要です

ログインが必要

管理者権限が必要

認証のやり方

/api/user/loginにログインリクエストを送ると、アクセストークンがクッキーに保管されます。

クッキーを送信する設定でリクエストを送ると認証できます。

Fetch APIを使用する場合

fetch(`${API_HOST}/api/user/check`, {credentials: 'include'})
    .then((r) => r.json())
    .then((d) => {
        console.log(`${d.user}としてログインしています`)
    })

Axiosを使用する場合

axios.get(`${API_HOST}/api/user/check`, {withCredentials: true})
    .then((r) => r.data)
    .then((d) => {
        console.log(`${d.user}としてログインしています`)
    })

POSTメソッドでのJSONデータの送信のやり方

ログインリクエストを送る例です。

Fetch APIを使用する場合

const data = {
    user_id: 'yamato1987',
    password: 'pass'
}
fetch(`${API_HOST}/api/user/login`, {
    method: 'post',
    body: JSON.stringify(data),
    headers: {
        'Content-Type': 'application/json'
    },
    credentials: 'include'
})
    .then((r) => r.json())
    .then((d) => console.log(`ログイン成功 ${d}`))

Axiosを使用する場合

const data = {
    user_id: 'yamato1987',
    password: 'pass'
}
axios.post(`${API_HOST}/api/user/login`, data, {withCredentials: true})
    .then((r) => r.data)
    .then((d) => console.log(`ログイン成功 ${d}`))

エンドポイント一覧

GET /api/category

カテゴリ一覧を返します。

POST /api/category/register

カテゴリを新規登録します。

リクエスト例

{
  "name": "料理"
}

POST /api/category/delete

カテゴリを削除します。

リクエスト例

{
  "id": 1
}

POST /api/category/edit

カテゴリ名を編集します。

リクエスト例

{
  "id": 1,
  "name": "勉強"
}

POST /api/user/register

ユーザーを登録します。

リクエスト例

{
  "user_id": "yamato1987",
  "password": "pass",
  "name": "山田 大和",
  "age": 34,
  "mail": "[email protected]",
  "gender": 0
}

POST /api/user/login

ユーザーIDとパスワードを入れて、正しければ認証に必要なトークンを発行します。

リクエスト例

{
  "user_id": "yamato1987",
  "password": "pass"
}

GET /api/user/logout

ログアウトします。

具体的には、クッキーからアクセストークンを削除します。

GET /api/user/check

ログインしていればステータスコード200を返します。

そうでなければステータスコード403を返します。

GET /api/user/checkadmin

管理者権限があればステータスコード200を返します。

そうでなければステータスコード403を返します。

GET /api/user/info/:user_id

ユーザーの情報を返します。

レスポンス例

{
  "user_id": "admin",
  "name": "Administrator",
  "age": 20,
  "gender": 0,
  "like": 0,
  "admin": true
}

GET /api/user/ranking

ユーザーを情報をいいね数の多い順で返します。

GET /api/question

質問一覧を返します。 回答はベストアンサーのみ返します。

質問は日付が新しい順でソートされます。

次のクエリパラメータが使用できます。

パラメータ 説明
query 検索キーワード
複数指定する場合はスペースで区切る
user_id 投稿者のユーザーID
c_id カテゴリ

リクエスト例

カテゴリID10番の中で「クレヨンしんちゃん」か「映画」を含む質問を返します。

/api/question?query=クレヨンしんちゃん+映画&c_id=10

POST /api/question/new

質問を投稿します。

リクエスト例

{
  "c_id": 11,
  "title": "インド旅行について教えてください!",
  "q_text": "インドに行くのが初めてなので、観光スポットやおすすめの場所、注意すべきことなど、インド旅行に関するアドバイスを教えていただきたいです。"
}

POST /api/question/delete

質問を削除します。この質問に対する回答も全て削除します。

リクエスト例

{
  "id": 1
}

GET /api/question/:q_id

q_idに該当する質問を返します。 回答は全ての回答を返します。

回答は日付が古い順、いいねが多い順でソートされます。

POST /api/question/best

質問のベストアンサーを設定します。質問の投稿者のみ設定できます。

リクエスト例

{
  "q_id": 1,
  "a_id": 3
}

POST /api/question/like

質問にいいねします。

リクエスト例

{
  "id": 1
}

POST /api/question/unlike

質問へのいいねを解除します。

リクエスト例

{
  "id": 1
}

POST /api/question/liked

質問へいいねしているかどうかを返します。

リクエスト例

{
  "id": 1
}

レスポンス例

{
  "liked": true
}

POST /api/answer/new

回答を投稿します。

リクエスト例

{
  "q_id": 1,
  "a_text": "タージマハルは必見です!日の出や日没の時間帯に訪れると美しい景色を楽しめます。"
}

POST /api/answer/delete

回答を削除します。投稿した本人か、その質問の投稿者のみ削除できます。

リクエスト例

{
  "id": 1
}

POST /api/answer/like

回答にいいねします。

リクエスト例

{
  "id": 1
}

POST /api/answer/unlike

回答へのいいねを解除します。

リクエスト例

{
  "id": 1
}

POST /api/answer/liked

回答へいいねしているかどうかを返します。

リクエスト例

{
  "id": 1
}

レスポンス例

{
  "liked": true
}

GET /api/icons/:user_id

ユーザーアイコンを返します。

qaapi-pbl's People

Contributors

itc-s21009 avatar itc-s22020 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.