Skip to content
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

go: ryanlee good-night-2nd-hackthon pull request #1

Open
wants to merge 6 commits into
base: main
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gorm.db
36 changes: 36 additions & 0 deletions Models/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package Models

import (
"time"
)

type Movies struct {
ID uint `json:"id"`
Title string `json:"title"`
Genre string `json:"genre"`
Start string `json:"start"`
End string `json:"end"`
Active bool `json:"active"`
IsDelete bool `json:"is_delete"`
UpdatedAt time.Time `json:"updated_at"`
CreatedAt time.Time `json:"created_at"`
AvgRating float64 `json:"avg_rating"`
Reviews []Reviews `json:"reviews" gorm:"foreignkey:MovieID"`
}

type Reviews struct {
ID uint `json:"id"`
MovieID int `json:"movie_id"`
Description string `json:"description"`
Rating float64 `json:"rating"`
UpdatedAt time.Time `json:"updated_at"`
CreatedAt time.Time `json:"created_at"`
}

func (b *Reviews) TableName() string {
return "reviews"
}

func (b *Movies) TableName() string {
return "movies"
}
77 changes: 77 additions & 0 deletions Models/movies.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package Models

import (
"math"
"strconv"

"github.com/printSANO/Good-Night-2nd-Hackathon-Backend/Config"
_ "gorm.io/driver/sqlite"
)

func GetAllMovies(movie *[]Movies) (err error) {
if err := Config.DB.Where("is_delete = ?", false).Order("start ASC").Find(movie).Error; err != nil {
return err
}
return nil
}

func GetAMovie(movie *Movies, id string) (err error) {
if err := Config.DB.Where("id = ?", id).First(movie).Error; err != nil {
return err
}
if err := Config.DB.Where("movie_id = ?", id).Find(&movie.Reviews).Error; err != nil {
return err
}
return nil
}

func GetAverageRating(reviews []Reviews, movieID string) (float64, error) {
var avgRating float64
var d float64
if err := Config.DB.Where("movie_id = ?", movieID).Find(&reviews).Error; err != nil {
return 0, err
}
for i, star := range reviews {
avgRating = avgRating + star.Rating
d = float64(i)
}
avgRating = avgRating / d
return math.Round(avgRating*10) / 10, nil
}

func GetMoviesByGenre(movie *[]Movies, genre string) (err error) {
if err := Config.DB.Where("genre = ?", genre).Order("start ASC").Find(movie).Error; err != nil {
return err
}
return nil
}

func GetMoviesByActive(movie *[]Movies, active string) (err error) {
b, err := strconv.ParseBool(active)
if err != nil {
return err
}
if err := Config.DB.Where("active = ?", b).Order("start ASC").Find(movie).Error; err != nil {
return err
}
return nil
}

func CreateAMovie(movie *Movies) (err error) {
if err := Config.DB.Create(movie).Error; err != nil {
return err
}
return nil
}

func DeleteAMovie(movie *Movies, id string) (err error) {
if err := Config.DB.Model(movie).Where("is_delete = ?", false).Update("is_delete", true).Error; err != nil {
return err
}
return nil
}

func UpdateAMovie(movie *Movies, id string) (err error) {
Config.DB.Where("is_delete = ?", false).Save(movie)
return nil
}
27 changes: 27 additions & 0 deletions Models/reviews.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package Models

import (
"github.com/printSANO/Good-Night-2nd-Hackathon-Backend/Config"
_ "gorm.io/driver/sqlite"
)

func GetReviewsByID(review *[]Reviews, id string) (err error) {
if err := Config.DB.Where("movie_id = ?", id).Order("updated_at DESC").Find(review).Error; err != nil {
return err
}
return nil
}

func GetReviewsByStars(review *[]Reviews, stars string) (err error) {
if err := Config.DB.Where("rating > ?", stars).Order("updated_at DESC").Find(review).Error; err != nil {
return err
}
return nil
}

func CreateReview(review *Reviews) (err error) {
if err := Config.DB.Create(review).Error; err != nil {
return err
}
return nil
}
7 changes: 7 additions & 0 deletions config/db-config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package Config

import (
"github.com/jinzhu/gorm"
)

var DB *gorm.DB
132 changes: 132 additions & 0 deletions controllers/movies.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package Controllers

import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/printSANO/Good-Night-2nd-Hackathon-Backend/Models"
)

func GetMovies(c *gin.Context) {
id := c.Query("id")
genre := c.Query("genre")
active := c.Query("active")
switch {
case id != "":
var movie Models.Movies
err := Models.GetAMovie(&movie, id)
if err != nil {
c.AbortWithStatus(http.StatusNotFound)
} else {
var reviews []Models.Reviews
err := Models.GetReviewsByID(&reviews, id)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
avgStars, err := Models.GetAverageRating(reviews, id)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
movie.AvgRating = avgStars
c.JSON(http.StatusOK, movie)
}
case genre != "" && active != "":
var movie []Models.Movies
err := Models.GetMoviesByGenre(&movie, genre)
if err != nil {
c.AbortWithStatus(http.StatusNotFound)
}
err = Models.GetMoviesByActive(&movie, active)
if err != nil {
c.AbortWithStatus(http.StatusNotFound)
} else {
c.JSON(http.StatusOK, movie)
}
case genre != "":
var movie []Models.Movies
err := Models.GetMoviesByGenre(&movie, genre)
if err != nil {
c.AbortWithStatus(http.StatusNotFound)
} else {
c.JSON(http.StatusOK, movie)
}
case active != "":
var movie []Models.Movies
err := Models.GetMoviesByActive(&movie, active)
if err != nil {
c.AbortWithStatus(http.StatusNotFound)
} else {
c.JSON(http.StatusOK, movie)
}
default:
var movie []Models.Movies
err := Models.GetAllMovies(&movie)
if err != nil {
c.AbortWithStatus(http.StatusNotFound)
} else {
c.JSON(http.StatusOK, movie)
}
}
}

func CreateAMovie(c *gin.Context) {
var movie Models.Movies
c.BindJSON(&movie)
title := movie.Title
if title == "" {
c.AbortWithStatus(http.StatusBadRequest)
} else {
err := Models.CreateAMovie(&movie)
if err != nil {
c.AbortWithStatus(http.StatusNotFound)
} else {
c.JSON(http.StatusOK, movie)
}
}
}

func DeleteAMovie(c *gin.Context) {
var movie Models.Movies
id := c.Query("id")
err := Models.GetAMovie(&movie, id)
if err != nil {
c.JSON(http.StatusNotFound, movie)
}
c.BindJSON(&movie)
err = Models.DeleteAMovie(&movie, id)
if err != nil {
c.AbortWithStatus(http.StatusNotFound)
} else {
c.JSON(http.StatusOK, movie)
}
}

func UpdateAMovie(c *gin.Context) {
var movie Models.Movies
id := c.Query("id")
err := Models.GetAMovie(&movie, id)
if err != nil {
c.JSON(http.StatusNotFound, movie)
}
c.BindJSON(&movie)
err = Models.UpdateAMovie(&movie, id)
if err != nil {
c.AbortWithStatus(http.StatusNotFound)
} else {
c.JSON(http.StatusOK, movie)
}
}

func GetMoviesByGenre(c *gin.Context) {
var movie []Models.Movies
genre := c.Query("id")
err := Models.GetMoviesByGenre(&movie, genre)
if err != nil {
c.AbortWithStatus(http.StatusNotFound)
} else {
c.JSON(http.StatusOK, movie)
}

}
46 changes: 46 additions & 0 deletions controllers/reviews.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package Controllers

import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/printSANO/Good-Night-2nd-Hackathon-Backend/Models"
)

func GetReviews(c *gin.Context) {
movieID := c.Query("movieID")
stars := c.Query("stars")
var review []Models.Reviews

err := Models.GetReviewsByID(&review, movieID)
if err != nil {
c.AbortWithStatus(http.StatusNotFound)
} else {
if stars != "" {
err := Models.GetReviewsByStars(&review, stars)
if err != nil {
c.AbortWithStatus(http.StatusNotFound)
} else {
c.JSON(http.StatusOK, review)
}
} else {
c.JSON(http.StatusOK, review)
}
}
}

func CreateReview(c *gin.Context) {
var review Models.Reviews
c.BindJSON(&review)
movieId := review.Description
if movieId == "" {
c.AbortWithStatus(http.StatusBadRequest)
} else {
err := Models.CreateReview(&review)
if err != nil {
c.AbortWithStatus(http.StatusNotFound)
} else {
c.JSON(http.StatusOK, review)
}
}
}
38 changes: 38 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module github.com/printSANO/Good-Night-2nd-Hackathon-Backend

go 1.20

require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.9.1 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/go-sql-driver/mysql v1.7.1 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/jinzhu/gorm v1.9.16 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/driver/sqlite v1.5.3 // indirect
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55 // indirect
)
Loading