Skip to content
This repository was archived by the owner on Dec 7, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ REPO_ROOT := $(shell git rev-parse --show-toplevel)
# SERVICES is the list services to test, services are located at $(REPO_ROOT)/service/<service_name>
# GATEWAYS is the list of top level directories to test, gateways are located at $(REPO_ROOT)/<gateway_name>
# Add new services or top level directories to test here
SERVICES := auth user registration decision rsvp checkin upload mail event stat notifications project profile
SERVICES := auth user registration decision rsvp checkin upload mail event stat notifications project profile prize
GATEWAYS := gateway common

# UTILITIES is the list of utilities to build, utilities are located at $(REPO_ROOT)/utilities/<utility_name>
Expand Down
4 changes: 4 additions & 0 deletions config/dev_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"NOTIFICATIONS_SERVICE": "http://localhost:8012",
"PROJECT_SERVICE": "http://localhost:8013",
"PROFILE_SERVICE": "http://localhost:8014",
"PRIZE_SERVICE": "http://localhost:8015",

"GATEWAY_PORT": "8000",
"AUTH_PORT": ":8002",
Expand All @@ -27,6 +28,7 @@
"NOTIFICATIONS_PORT": ":8012",
"PROJECT_PORT": ":8013",
"PROFILE_PORT": ":8014",
"PRIZE_PORT": ":8015",

"AUTH_DB_HOST": "localhost",
"USER_DB_HOST": "localhost",
Expand All @@ -41,6 +43,7 @@
"NOTIFICATIONS_DB_HOST": "localhost",
"PROJECT_DB_HOST": "localhost",
"PROFILE_DB_HOST": "localhost",
"PRIZE_DB_HOST": "localhost",

"AUTH_DB_NAME": "auth",
"USER_DB_NAME": "user",
Expand All @@ -55,6 +58,7 @@
"NOTIFICATIONS_DB_NAME": "notifications",
"PROJECT_DB_NAME": "project",
"PROFILE_DB_NAME": "profile",
"PRIZE_DB_NAME": "prize",

"S3_REGION": "us-east-1",
"S3_BUCKET": "hackillinois-upload-2019",
Expand Down
4 changes: 4 additions & 0 deletions config/test_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"NOTIFICATIONS_SERVICE": "http://localhost:8012",
"PROJECT_SERVICE": "http://localhost:8013",
"PROFILE_SERVICE": "http://localhost:8014",
"PRIZE_SERVICE": "http://localhost:8015",

"GATEWAY_PORT": "8000",
"AUTH_PORT": ":8002",
Expand All @@ -27,6 +28,7 @@
"NOTIFICATIONS_PORT": ":8012",
"PROJECT_PORT": ":8013",
"PROFILE_PORT": ":8014",
"PRIZE_PORT": ":8015",

"AUTH_DB_HOST": "localhost",
"USER_DB_HOST": "localhost",
Expand All @@ -41,6 +43,7 @@
"NOTIFICATIONS_DB_HOST": "localhost",
"PROJECT_DB_HOST": "localhost",
"PROFILE_DB_HOST": "localhost",
"PRIZE_DB_HOST": "localhost",

"AUTH_DB_NAME": "test-auth",
"USER_DB_NAME": "test-user",
Expand All @@ -55,6 +58,7 @@
"NOTIFICATIONS_DB_NAME": "test-notifications",
"PROJECT_DB_NAME": "test-project",
"PROFILE_DB_NAME": "test-profile",
"PRIZE_DB_NAME": "test-prize",

"S3_REGION": "us-east-1",
"S3_BUCKET": "hackillinois-upload-2019",
Expand Down
7 changes: 7 additions & 0 deletions gateway/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var STAT_SERVICE string
var NOTIFICATIONS_SERVICE string
var PROJECT_SERVICE string
var PROFILE_SERVICE string
var PRIZE_SERVICE string

func Initialize() error {

Expand Down Expand Up @@ -119,6 +120,12 @@ func Initialize() error {
return err
}

PRIZE_SERVICE, err = cfg_loader.Get("PRIZE_SERVICE")

if err != nil {
return err
}

port_str, err := cfg_loader.Get("GATEWAY_PORT")

if err != nil {
Expand Down
76 changes: 76 additions & 0 deletions gateway/services/prize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package services

import (
"net/http"

"github.com/HackIllinois/api/gateway/config"
"github.com/HackIllinois/api/gateway/middleware"
"github.com/HackIllinois/api/gateway/models"
"github.com/arbor-dev/arbor"
"github.com/justinas/alice"
)

const PrizeFormat string = "JSON"

var PrizeRoutes = arbor.RouteCollection{
arbor.Route{
"GetPrize",
"GET",
"/prize/",
alice.New(middleware.AuthMiddleware([]models.Role{models.AdminRole, models.StaffRole}), middleware.IdentificationMiddleware).ThenFunc(GetPrize).ServeHTTP,
},
arbor.Route{
"CreatePrize",
"POST",
"/prize/",
alice.New(middleware.AuthMiddleware([]models.Role{models.AdminRole, models.StaffRole}), middleware.IdentificationMiddleware).ThenFunc(CreatePrize).ServeHTTP,
},
arbor.Route{
"UpdatePrize",
"PUT",
"/prize/",
alice.New(middleware.AuthMiddleware([]models.Role{models.AdminRole, models.StaffRole}), middleware.IdentificationMiddleware).ThenFunc(UpdatePrize).ServeHTTP,
},
arbor.Route{
"DeletePrize",
"DELETE",
"/prize/",
alice.New(middleware.AuthMiddleware([]models.Role{models.AdminRole, models.StaffRole}), middleware.IdentificationMiddleware).ThenFunc(DeletePrize).ServeHTTP,
},
arbor.Route{
"AwardPoints",
"POST",
"/prize/points/award/",
alice.New(middleware.AuthMiddleware([]models.Role{models.AdminRole, models.StaffRole}), middleware.IdentificationMiddleware).ThenFunc(AwardShopPoints).ServeHTTP,
},
arbor.Route{
"RedeemPoints",
"POST",
"/prize/points/redeem/",
alice.New(middleware.AuthMiddleware([]models.Role{models.AdminRole, models.StaffRole}), middleware.IdentificationMiddleware).ThenFunc(RedeemShopPoints).ServeHTTP,
},
}

func GetPrize(w http.ResponseWriter, r *http.Request) {
arbor.GET(w, config.PRIZE_SERVICE+r.URL.String(), PrizeFormat, "", r)
}

func CreatePrize(w http.ResponseWriter, r *http.Request) {
arbor.POST(w, config.PRIZE_SERVICE+r.URL.String(), PrizeFormat, "", r)
}

func UpdatePrize(w http.ResponseWriter, r *http.Request) {
arbor.PUT(w, config.PRIZE_SERVICE+r.URL.String(), PrizeFormat, "", r)
}

func DeletePrize(w http.ResponseWriter, r *http.Request) {
arbor.DELETE(w, config.PRIZE_SERVICE+r.URL.String(), PrizeFormat, "", r)
}

func AwardShopPoints(w http.ResponseWriter, r *http.Request) {
arbor.POST(w, config.PRIZE_SERVICE+r.URL.String(), PrizeFormat, "", r)
}

func RedeemShopPoints(w http.ResponseWriter, r *http.Request) {
arbor.POST(w, config.PRIZE_SERVICE+r.URL.String(), PrizeFormat, "", r)
}
2 changes: 2 additions & 0 deletions gateway/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func Initialize() error {
"notifications": config.NOTIFICATIONS_SERVICE,
"project": config.PROJECT_SERVICE,
"profile": config.PROFILE_SERVICE,
"prize": config.PRIZE_SERVICE,
}

return nil
Expand Down Expand Up @@ -70,6 +71,7 @@ func RegisterAPIs() arbor.RouteCollection {
Routes = append(Routes, ProfileRoutes...)
Routes = append(Routes, HealthRoutes...)
Routes = append(Routes, ReloadRoutes...)
Routes = append(Routes, PrizeRoutes...)
return Routes
}

Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/HackIllinois/api/services/event"
"github.com/HackIllinois/api/services/mail"
"github.com/HackIllinois/api/services/notifications"
"github.com/HackIllinois/api/services/prize"
"github.com/HackIllinois/api/services/profile"
"github.com/HackIllinois/api/services/project"
"github.com/HackIllinois/api/services/registration"
Expand All @@ -38,6 +39,7 @@ var SERVICE_ENTRYPOINTS = map[string](func()){
"notifications": notifications.Entry,
"project": project.Entry,
"profile": profile.Entry,
"prize": prize.Entry,
}

func StartAll() {
Expand Down
1 change: 1 addition & 0 deletions scripts/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ $REPO_ROOT/bin/hackillinois-api --service stat &
$REPO_ROOT/bin/hackillinois-api --service notifications &
$REPO_ROOT/bin/hackillinois-api --service project &
$REPO_ROOT/bin/hackillinois-api --service profile &
$REPO_ROOT/bin/hackillinois-api --service prize &

$REPO_ROOT/bin/hackillinois-api --service gateway &

Expand Down
40 changes: 40 additions & 0 deletions services/prize/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package config

import (
"os"

"github.com/HackIllinois/api/common/configloader"
)

var PRIZE_DB_HOST string
var PRIZE_DB_NAME string

var PRIZE_PORT string

func Initialize() error {
cfg_loader, err := configloader.Load(os.Getenv("HI_CONFIG"))

if err != nil {
return err
}

PRIZE_DB_HOST, err = cfg_loader.Get("PRIZE_DB_HOST")

if err != nil {
return err
}

PRIZE_DB_NAME, err = cfg_loader.Get("PRIZE_DB_NAME")

if err != nil {
return err
}

PRIZE_PORT, err = cfg_loader.Get("PRIZE_PORT")

if err != nil {
return err
}

return nil
}
134 changes: 134 additions & 0 deletions services/prize/controller/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package controller

import (
"encoding/json"
"net/http"

"github.com/HackIllinois/api/common/errors"
"github.com/HackIllinois/api/services/prize/models"
"github.com/HackIllinois/api/services/prize/service"
"github.com/gorilla/mux"
)

func SetupController(route *mux.Route) {
router := route.Subrouter()

router.HandleFunc("/", GetPrize).Methods("GET")
router.HandleFunc("/", CreatePrize).Methods("POST")
router.HandleFunc("/", UpdatePrize).Methods("PUT")
router.HandleFunc("/", DeletePrize).Methods("DELETE")

router.HandleFunc("/points/award/", AwardShopPoints).Methods("POST")
router.HandleFunc("/points/redeem/", RedeemShopPoints).Methods("POST")
}

/*
GetPrize is the endpoint to get a prize from the given prize id.
*/
func GetPrize(w http.ResponseWriter, r *http.Request) {
var request models.GetPrizeRequest
json.NewDecoder(r.Body).Decode(&request)

prize, err := service.GetPrize(request.ID)

if err != nil {
errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not get prize id"))
return
}

json.NewEncoder(w).Encode(prize)
}

func CreatePrize(w http.ResponseWriter, r *http.Request) {
var prize models.Prize
json.NewDecoder(r.Body).Decode(&prize)

err := service.CreatePrize(prize)

if err != nil {
errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not create a new prize"))
return
}

json.NewEncoder(w).Encode(prize)
}

func UpdatePrize(w http.ResponseWriter, r *http.Request) {
var prize models.Prize
json.NewDecoder(r.Body).Decode(&prize)

err := service.UpdatePrize(prize.ID, prize)

if err != nil {
errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not update a new prize"))
return
}

json.NewEncoder(w).Encode(prize)
}

func DeletePrize(w http.ResponseWriter, r *http.Request) {
var request models.DeletePrizeRequest
json.NewDecoder(r.Body).Decode(&request)

prize, err := service.GetPrize(request.ID)

if err != nil {
errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Prize does not exist"))
return
}

err = service.DeletePrize(request.ID)

if err != nil {
errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not delete prize"))
return
}

json.NewEncoder(w).Encode(prize)
}

/*
AwardPoints gives the specified number of points to the current user.
*/
func AwardShopPoints(w http.ResponseWriter, r *http.Request) {
var request models.AwardPointsRequest
json.NewDecoder(r.Body).Decode(&request)

id := request.ID
add_points := request.ShopPoints

user_points, err := service.AwardPoints(add_points, id)

if err != nil {
errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not update the user's points"))
return
}

json.NewEncoder(w).Encode(user_points)
}

/*
RedeemPoints attempts to give the specified item to the user for points.
*/
func RedeemShopPoints(w http.ResponseWriter, r *http.Request) {
var request models.RedeemPointsRequest
json.NewDecoder(r.Body).Decode(&request)

id := request.ID
item_id := request.ShopItemID

err := service.RedeemPrize(item_id, id)

if err != nil {
errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not get prize of id \""+item_id+"\" for user \""+id+"\"."))
return
}

// Could have return UserPoints struct instead
res := models.RedeemPointsResponse{
Status: "success",
}

json.NewEncoder(w).Encode(res)
}
Loading