Skip to content

linus-sh/jimm1-solo-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

まえがき

このリポジトリはサーバー構築、及び RESTful API の勉強のために作成されたものです。 このリポジトリに含まれる一切はコードクリサリスの生徒だった際に作成したものです。 あくまで学習用に作成したものなので使用する際はその点留意してください。 またこのリポジトリのに含まれる一切の使用に関して、問題、障害など発生しても作者は一切の責任を負いかねますので、使用にあたっては十分にご留意ください。

使用技術

このプログラムは JavaScript と NodeJs ライブラリを用いて作成しました。 使用したパッケージ等は以下のとおりです。 ・NodeJS :https://nodejs.org/ja/ ・Express :https://expressjs.com/ ・postgress :https://www.postgresql.org/ ・knex :http://knexjs.org/

環境構築

依存パッケージダウンロード

NodeJS(https://nodejs.org/ja/)と Postgress(https://www.postgresql.org/)がインストールされていない場合は、それぞれ公式サイトからダウンロード、インストールしてください。

インストールが完了したらターミナルでyarnを実行して、依存パッケージをインストールしてください。

サーバー起動

NodeJS と Express を用いてサーバーをlcoalhostに構築します。 ターミナルでyarn devを実行すると、localhost:3000でサーバーが起動します。

DB セットアップ

このリポジトリでは postgres を利用してデータベースを構築します。作成したデータベースに対して各 API がアクセスしてデータの取得、登録、更新、削除を行います。 Postgress のインストール後、ターミナルで以下のコマンドを実行して、データベースを作成してください。 echo "CREATE DATABASE solo-api;" | psql

マイグレーション / ロールバック

それぞれターミナルからyarn migrateyarn rollbackのコマンドで実行できます。 yarn migrateを実行すると以下のテーブルが作成されます.

・users "id" : interger, autoincrement, notnullable, index "username" : string(maxmam 20 chars), notNullable,unique,index "height" : float(8 byte, 4 digits), notNullable "create_at" : timestamp, notNullable, default(date.now())

・records "id" : interger, autoincrement, notnullable, index "user_id" : integer, references(table: "users", culomn: "id"),notNullable "date" : date, notNullable "weights" : float(8 byte, 3 digits), notNullable "bmi" : float(8 byte, 3 digits), notNullable "sleeping" : float(8 byte, 3 digits), notNullable "record_at" : timestamp, notNullable, default(date.now())

シードデータ挿入

ターミナルでyarn seedを実行すると、動作確認用の初期データをデータベースに挿入できます。

各 API の動作

サーバー起動状態でlcoalhost:3000にアクセスすると各 API の動作確認用のボタンを配置した HTML ページが表示されます。

users テーブル関連

・「Get User List」ボタン users テーブルに登録された全ユーザーデータを取得します。 http://localhost:3000/api/usersに対して GET メソッドを実行します。

・「Register New User」ボタン users テーブルに新規ユーザーを追加します。 http://localhost:3000/api/usersに対して以下の形式で新規ユーザー情報を含んだ body を json 形式で POST すると実行されます。

body { name: new_user_name, height: nse_users_height }

・「Fix User Data」ボタン 登録済みのユーザー情報を更新します。 http://localhost:3000/api/usersに対して以下の形式で新規ユーザー情報を含んだ body を json 形式で PATCH すると実行されます。一度に変更できるのは username と height のどちらか一方のみです。 body { name :target_user_name, newUserName :new_user_name, newHeight:  new_user_height }

・「Delete User Data」ボタン 登録済みユーザーから指定のユーザーを削除します。 http://localhost:3000/api/users/:nameに対して DELETE をすると実行されます。対象のユーザーは:nameで指定 します。

recoeds テーブル関連

※以下は未実装です

・「Get Record By User」ボタン 指定したユーザーの記録を取得します。 http://localhost:3000/api/record/:idに対して GET すると実行されます。対象のユーザーは:idでユーザー ID で指定します。

・「Register New Record」ボタン 指定したユーザーの健康情報を登録します。 http://localhost:3000/api/record/:name に対して以下の形式で新規ユーザー情報を含んだ body を json 形式で POST すると実行されます。対象のユーザーは:nameで指定します。

body { date: "target_record_Datae", weights: "target_weight", sleeping: "target_sleep_time(hour)" },

・「Fix record」ボタン 登録済みの健康情報を修正します。 http://localhost:3000/api/record/:id に対して以下の形式で新規ユーザー情報を含んだ body を json 形式で PATACH すると実行されます。対象の record の id は:idで指定します。一度の PATCH で修正できるのは date,weights,sleeping のうち一つだけです。

body { date: "target_record_Datae", weights: "target_weight", sleeping: "target_sleep_time(hour)" },

・「Delete recorde」ボタン 健康管理情報を削除します。 http://localhost:3000/api/record/:name/:date に対して DELETE すると実行されます。対象のユーザーは:nameで、日付をddateで指定します。

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published