Skip to content

Commit

Permalink
Merge pull request #4 from fujikawahiroaki/develop
Browse files Browse the repository at this point in the history
feat: collection_settingsのCRUD完成
  • Loading branch information
fujikawahiroaki committed Jun 3, 2024
2 parents 37bddf6 + 411306a commit 0811387
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 16 deletions.
2 changes: 1 addition & 1 deletion dc-lucky.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
docker-compose exec lucky lucky $1
docker-compose exec lucky lucky ${@}
2 changes: 0 additions & 2 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ targets:
specbox_api:
main: src/specbox_api.cr
crystal: '>= 1.10.1'
crystalline:
main: src/specbox_api.cr
dependencies:
lucky:
github: luckyframework/lucky
Expand Down
6 changes: 6 additions & 0 deletions src/actions/api/collection_settings/create.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Api::CollectionSettings::Create < ApiAction
post "/api/collection-settings/own-collection-settings" do
collection_setting = SaveCollectionSetting.create!(params, id: UUID.random, user_id: current_user.id)
head HTTP::Status::CREATED
end
end
11 changes: 11 additions & 0 deletions src/actions/api/collection_settings/delete.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Api::CollectionSettings::Delete < ApiAction
delete "/api/collection-settings/own-collection-settings/:collection_setting_id" do
collection_setting = CollectionSettingQuery.new.find(collection_setting_id)
if collection_setting.user_id == current_user.id
DeleteCollectionSetting.delete!(collection_setting)
head 204
else
head 403
end
end
end
36 changes: 34 additions & 2 deletions src/actions/api/collection_settings/index.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,38 @@
class Api::CollectionSettings::Index < ApiAction
get "/api/collection-settings/own-collection-settings" do
collection_settings = CollectionSettingQuery.new.user_id(current_user.id)
json CollectionSettingSerializer.for_collection(collection_settings)
query = CollectionSettingQuery.new.user_id(current_user.id)
if params.get?(:collection_name)
query = query.collection_name.ilike("%#{params.get(:collection_name)}%")
end
if params.get?(:institution_code)
query = query.institution_code.ilike("%#{params.get(:institution_code)}%")
end
if params.get?(:latest_collection_code_min)
query = query.latest_collection_code.gte(params.get(:latest_collection_code_min))
end
if params.get?(:latest_collection_code_max)
query = query.latest_collection_code.lte(params.get(:latest_collection_code_max))
end
if params.get?(:note)
query = query.note.ilike("%#{params.get(:note)}%")
end
if params.get?(:created_at)
query = query.created_at.as_date.eq(params.get(:created_at))
end
if params.get?(:sort)
sort_column = params.get(:sort)
if sort_column == "collection_name"
query = switch_order(query.collection_name, params)
elsif sort_column == "institution_code"
query = switch_order(query.institution_code, params)
elsif sort_column == "latest_collection_code"
query = switch_order(query.latest_collection_code, params)
else
query = switch_order(query.created_at, params)
end
else
query = switch_order(query.created_at, params)
end
json CollectionSettingSerializer.for_collection(query)
end
end
11 changes: 11 additions & 0 deletions src/actions/api/collection_settings/update.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Api::CollectionSettings::Update < ApiAction
put "/api/collection-settings/own-collection-settings/:collection_setting_id" do
collection_setting = CollectionSettingQuery.new.find(collection_setting_id)
if collection_setting.user_id == current_user.id
SaveCollectionSetting.update!(collection_setting, params)
head 204
else
head 403
end
end
end
1 change: 1 addition & 0 deletions src/actions/api_action.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ abstract class ApiAction < Lucky::Action
# Add 'include Lucky::SkipRouteStyleCheck' to your actions if you wish to ignore this check for specific routes.
# include Lucky::EnforceUnderscoredRoute
include Lucky::SkipRouteStyleCheck
include Api::Custom::SwitchOrder
end
1 change: 0 additions & 1 deletion src/actions/mixins/api/auth/auth0_helpers.cr
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ module Api::Auth::Auth0Helpers
decoded_token = self.validate_token(token)
return nil if decoded_token.nil?
username = decoded_token[0]["sub"].as_s.sub("|", ".")
Log.info { "login_user: #{username}" }
user = AuthUserQuery.new.username(username).first?
if user.nil?
SaveAuthUser.create!(username: username, date_joined: Time.local)
Expand Down
8 changes: 8 additions & 0 deletions src/actions/mixins/api/custom/switch_order_by.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Api::Custom::SwitchOrder
def switch_order(query, params)
if params.get?(:order)
return query.desc_order if params.get(:order) == "DESC"
end
query.asc_order
end
end
2 changes: 1 addition & 1 deletion src/models/base_model.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ abstract class BaseModel < Avram::Model
end

macro default_columns
primary_key custom_key : UUID
primary_key id : UUID
timestamps
end
end
2 changes: 1 addition & 1 deletion src/models/collection_setting.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class CollectionSetting < BaseModel
# 機関コード(コレクション名の略号)
column institution_code : String = "", allow_blank: true
# 標本IDの最終番号
column latest_collection_code : Int64 = 0i64
column latest_collection_code : Int32 = 0
# 備考
column note : String = "", allow_blank: true
end
Expand Down
2 changes: 0 additions & 2 deletions src/operations/delete_collection_setting.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
class DeleteCollectionSetting < CollectionSetting::DeleteOperation
# Read more on deleting records
# https://luckyframework.org/guides/database/deleting-records
end
10 changes: 4 additions & 6 deletions src/operations/save_collection_setting.cr
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
class SaveCollectionSetting < CollectionSetting::SaveOperation
# To save user provided params to the database, you must permit them
# https://luckyframework.org/guides/database/saving-records#perma-permitting-columns
#
# permit_columns column_1, column_2
permit_columns(
id,
collection_name,
institution_code,
latest_collection_code,
note
note,
user_id
)

before_save do
validate_size_of collection_name, max: 174
validate_size_of institution_code, max: 10
validate_numeric latest_collection_code, less_than: 999999999999999999i64
validate_numeric latest_collection_code, no_more_than: 999999999999999999i64
end
end

0 comments on commit 0811387

Please sign in to comment.