Skip to content

kadai4 yasuno #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
23 changes: 23 additions & 0 deletions kadai4/yasu/controllers/rottely_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package controllers

import (
"dojo4/kadai4/yasu/controllers/serializer"
"dojo4/kadai4/yasu/services"
"encoding/json"
"net/http"
)

func DevineFortune(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fortune, err := services.Rottely()
if err != nil {
response := serializer.Error{Err: err}
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(response)
return
}

w.WriteHeader(http.StatusOK)
response := serializer.RottelyResult{Fortune: fortune}
json.NewEncoder(w).Encode(response)
}
29 changes: 29 additions & 0 deletions kadai4/yasu/controllers/rottely_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package controllers

import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)

func DevineFortuneTest(t *testing.T) {
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/rottely", nil)
DevineFortune(w, r)
res := w.Result()
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
t.Fatal("unexpected status code")
}

bytes, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Error(err)
t.Fatal("unexpected error")
}
if fortune := string(bytes); fortune == "" {
t.Fatalf("unexpected response: %s", fortune)
}
}
5 changes: 5 additions & 0 deletions kadai4/yasu/controllers/serializer/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package serializer

type Error struct {
Err error
}
5 changes: 5 additions & 0 deletions kadai4/yasu/controllers/serializer/rottely_serializer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package serializer

type RottelyResult struct {
Fortune string `json:"fortune"`
}
13 changes: 13 additions & 0 deletions kadai4/yasu/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"dojo4/kadai4/yasu/controllers"
"net/http"
)

func main() {

http.HandleFunc("/rottely", controllers.DevineFortune)

http.ListenAndServe(":8080", nil)
}
62 changes: 62 additions & 0 deletions kadai4/yasu/services/rottely_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package services

import (
"errors"
"math/rand"
"time"
)

func Rottely() (string, error) {
var randomInt int
var fortune string
var err error
location, _ := time.LoadLocation("Asia/Tokyo")
now := time.Now().In(location)
newYearsDay := time.Date(2018, 1, 1, 0, 0, 0, 0, location)
endDay := time.Date(2018, 1, 4, 0, 0, 0, 0, location)
if !now.Before(newYearsDay) && !now.After(endDay) {
fortune = "大吉"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else が長く続くならここで return "大吉", nil としたほうがネストが浅くなって良いです

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます!

} else {
rand.Seed(time.Now().UnixNano())
randomInt = rand.Intn(17)
switch randomInt {
case 0:
fortune = "大大吉"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この変数に入れてなにか処理をするとかもないので、さっと return "運勢", nil していいと思います。
default の場合は return "", errors.New(...) となります。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

確かにそうですね....
ありがとうございます!

case 1:
fortune = "大吉"
case 2:
fortune = "向大吉"
case 3:
fortune = "末大吉"
case 4:
fortune = "吉凶末分末大吉"
case 5:
fortune = "吉"
case 6:
fortune = "中吉"
case 7:
fortune = "小吉"
case 8:
fortune = "後吉"
case 9:
fortune = "末吉"
case 10:
fortune = "吉凶不分末吉"
case 11:
fortune = "末凶相交末吉"
case 12:
fortune = "吉凶相半"
case 13:
fortune = "吉凶相央"
case 14:
fortune = "小吉後吉"
case 15:
fortune = "凶後吉"
case 16:
fortune = "凶後大吉"
default:
err = errors.New("Rottely error: Invalid integer")
}
}
return fortune, err
}