Skip to content

Commit

Permalink
Merge pull request #12 from SEbbaDK/server-writing
Browse files Browse the repository at this point in the history
Added contribution class and contribution post endpoint
  • Loading branch information
SEbbaDK authored May 7, 2021
2 parents 72f29ca + 053de61 commit 91863d7
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 5 deletions.
29 changes: 29 additions & 0 deletions server/src/contribution.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Contribution
property contribution_id : Nil | Int64
property user_id : Int64
property type : Int64
property changeset : Int64
property score : Int64
property date_time : Time

def initialize (
@user_id,
@type,
@changeset,
@score,
@date_time,
@contribution_id = nil,
)
end

def self.from_json (json_obj)
#contribution_id: json_obj["contributionid"]?.as(Int64 | Nil),
contribution = Contribution.new(
user_id:json_obj["id"].as(Int64),
type: json_obj["type"].as(Int64),
changeset:json_obj["changeset"].as(Int64),
score: json_obj["score"].as(Int64),
date_time: Time.parse_iso8601(json_obj.["datetime"].as(String)),
)
end
end
45 changes: 40 additions & 5 deletions server/src/maptogether-server.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require "./user.cr"
require "./scoring.cr"
require "./queries.cr"
require "./achievement.cr"
require "./contribution.cr"


module MapTogether::Server
Expand All @@ -28,11 +29,24 @@ module MapTogether::Server
end
end

def try_open_connection(&block)
begin
yield DB.open "postgres://maptogether:maptogether@localhost:5432/maptogether?retry_attempts=100"
rescue exception : DB::ConnectionRefused
raise DB::ConnectionRefused.new("Could not connect to database")
end
end

#before_get do |env|
# env.response.content_type = "application/json"
#end

# Request data about a specific user (id, name, score, achievements and followers)
get "/user/:id" do |env|
user = User.new
id = env.params.url["id"]
DB.connect address do |db|

try_open_connection do |db|
user.user_id, user.name = db.query_one Queries::USER_FROM_ID, id, as: {Int64, String}
user.score = db.query_one Queries::TOTAL_SCORE_FROM_ID, id, as: {Int64}

Expand Down Expand Up @@ -66,7 +80,7 @@ module MapTogether::Server
# Retrieve all users' id, name and score
get "/leaderboard/global/all_time" do |env|
JSON.build do |json|
DB.connect address do |db|
try_open_connection do |db|
db.query Queries::GLOBAL_ALL_TIME do |rows|
Endpoints.leaderboard_to_json(json, rows)
end
Expand All @@ -76,7 +90,7 @@ module MapTogether::Server

get "/leaderboard/global/monthly" do |env|
JSON.build do |json|
DB.connect address do |db|
try_open_connection do |db|
db.query Queries::GLOBAL_INTERVAL, Time.utc.at_beginning_of_month, Time.utc.at_end_of_month do |rows|
Endpoints.leaderboard_to_json(json, rows)
end
Expand All @@ -86,18 +100,39 @@ module MapTogether::Server

get "/leaderboard/global/weekly" do |env|
JSON.build do |json|
DB.connect address do |db|
try_open_connection do |db|
db.query Queries::GLOBAL_INTERVAL, Time.utc.at_beginning_of_week, Time.utc.at_end_of_week do |rows|
Endpoints.leaderboard_to_json(json, rows)
end
end
end
end

post "/contribution" do |env|
contribution = Contribution.from_json(env.params.json)
try_open_connection do |db|
db.exec "INSERT INTO contributions (userID, type, changeset, score, dateTime)
VALUES ($1, $2, $3, $4, $5)",
contribution.user_id,
contribution.type,
contribution.changeset,
contribution.score,
contribution.date_time
end
end

# Test endpoint
get "/" do
get "/" do |env|
"hi"
end

error 500 do |env, exc|
"Something went wrong, \"#{exc.class}\" was thrown with message: \"#{exc.message}\""
end

error 404 do
"404 Path not defined"
end
end

port = (ENV["KEMAL_PORT"]? || 8080).to_i32
Expand Down

0 comments on commit 91863d7

Please sign in to comment.