Skip to content

Commit

Permalink
user and movie update
Browse files Browse the repository at this point in the history
  • Loading branch information
Dado555 committed Aug 17, 2022
1 parent deec318 commit 03e9af5
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 3 deletions.
9 changes: 9 additions & 0 deletions gleficu-frontend/src/components/movies/EditMovieModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ export default {
})
},
},
// update movie
// let payload = {
// imdbLink: "",
// rottenLink: "",
// torrentLinks: "",
// titleLinks: ""
// }
data() {
return {
finalModel: {},
Expand Down
8 changes: 8 additions & 0 deletions gleficu-frontend/src/components/users/EditProfileModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ export default {
}
}
},
// update user
// let payload = {
// gender: "",
// age: "",
// favouriteTags: ""
// }
data() {
return {
finalModel: {},
Expand Down
9 changes: 9 additions & 0 deletions gleficu-frontend/src/services/movieService.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const GET_ALL_MOVIES = API_URL + "getAllMovies"
const GET_MOVIE_BY_IMDB_ID = API_URL + "getByImdbId"
const WATCH_MAGNET = API_URL + "watchMagnet"
const SEARCH_MOVIES = API_URL + "searchMovies"
const UPDATE_MOVIE = API_URL + "updateMovie"

class MovieService {
getByTitle(movie) {
Expand Down Expand Up @@ -71,6 +72,14 @@ class MovieService {
},
})
}

updateMovie(movieId, payload) {
return axios.put(UPDATE_MOVIE + `/${movieId}`, payload, {
headers: {
Authorization: "Bearer " + localStorage.getItem("id_token"),
},
});
}
}

export const movieService = new MovieService();
9 changes: 9 additions & 0 deletions gleficu-frontend/src/services/userService.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const SEARCH_USERS = API_URL + 'searchUsers'
const GET_BY_USERNAME = API_URL + 'username'
const GET_BY_ID = API_URL + 'id'
const GET_USERS_PAGE = API_URL + 'getUsersPage'
const UPDATE_USER = API_URL + 'updateUser'

class UserService {
searchUsers(searchTerm) {
Expand Down Expand Up @@ -46,6 +47,14 @@ class UserService {
},
});
}

updateUser(userId, payload) {
return axios.put(UPDATE_USER + `/${userId}`, payload, {
headers: {
Authorization: "Bearer " + localStorage.getItem("id_token"),
},
});
}
}

export const userService = new UserService();
51 changes: 51 additions & 0 deletions scrape-movie-service/api/movies.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"fmt"
"github.com/Dado555/glef.icu/scrape-movie-service/downloadPlay"
"github.com/Dado555/glef.icu/scrape-movie-service/models"
"github.com/Dado555/glef.icu/scrape-movie-service/repository"
"github.com/Dado555/glef.icu/scrape-movie-service/service"
"github.com/gorilla/mux"
"net/http"
"regexp"
"strconv"
)

Expand Down Expand Up @@ -162,3 +164,52 @@ func (api *API) SearchMovies(w http.ResponseWriter, request *http.Request) {
return
}
}

func (api *API) UpdateMovie(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")

params := mux.Vars(req)
imdbId, _ := params["movieId"]

if !api.movies.HasMovie(imdbId) {
http.Error(w, "Movie does not exist", http.StatusBadRequest)
return
}

decoder := json.NewDecoder(req.Body)
jsonData := models.MovieUpdateDTO{}
err := decoder.Decode(&jsonData)

if err != nil {
http.Error(w, "Update data is wrong", http.StatusBadRequest)
return
}

movie := api.movies.FindMovieByImdbID(imdbId)
if len(jsonData.ImdbLink) > 10 {
// parse link and get imdbID
// get movie data with omdb
re := regexp.MustCompile("tt\\d{6,9}")
match := re.FindStringSubmatch(jsonData.ImdbLink)
if match == nil {
http.Error(w, "Not valid imdb link", http.StatusBadRequest)
return
}
updateMovie := service.FindByImdbId(match[0])
models.UpdateMovieData(movie, &updateMovie)
torrentLink, subtitleLink := repository.FindTorrentMagnetAndSubtitleLink(updateMovie)
movie.TorrentLinks = torrentLink
movie.TitleLinks = subtitleLink
}
if len(jsonData.RottenLink) > 10 {
// rotten link
}
if len(jsonData.TorrentLinks) > 10 {
movie.TorrentLinks = jsonData.TorrentLinks
}
if len(jsonData.TitleLinks) > 10 {
movie.TitleLinks = jsonData.TitleLinks
}

api.movies.UpdateMovie(movie)
}
28 changes: 28 additions & 0 deletions scrape-movie-service/models/movie.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,31 @@ type MovieDb struct {
TorrentLinks string `json:"torrentLinks"`
TitleLinks string `json:"titleLinks"`
}

type MovieUpdateDTO struct {
ImdbLink string `json:"imdbLink"`
RottenLink string `json:"rottenLink"`
TorrentLinks string `json:"torrentLinks"`
TitleLinks string `json:"titleLinks"`
}

func UpdateMovieData(original *MovieDb, new *Movie) {
original.Title = new.Title
original.ImdbID = new.ImdbID
original.Language = new.Language
original.ImdbVotes = new.ImdbVotes
original.Metascore = new.Metascore
original.ImdbRating = new.ImdbRating
original.Poster = new.Poster
original.Country = new.Country
original.Plot = new.Plot
original.Actors = new.Actors
original.Writer = new.Writer
original.Director = new.Director
original.Genre = new.Genre
original.Runtime = new.Runtime
original.Released = new.Released
original.Rated = new.Rated
original.Year = new.Year
original.Type = new.Type
}
14 changes: 11 additions & 3 deletions scrape-movie-service/repository/movieRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ func NewMovieManager(db *models.DB) (*MovieManager, error) {
return &movieMgr, nil
}

// AddMovie - Creates a movie
func (state *MovieManager) AddMovie(movie models.Movie) *models.MovieDb {
func FindTorrentMagnetAndSubtitleLink(movie models.Movie) (string, string) {
// Create new client
yts := imdb2torrent.NewYTSclient(imdb2torrent.DefaultYTSclientOpts, imdb2torrent.NewInMemoryCache(), zap.NewNop(), false)

Expand Down Expand Up @@ -81,7 +80,12 @@ func (state *MovieManager) AddMovie(movie models.Movie) *models.MovieDb {
//fmt.Println(subtitles)
}

// etc.
return torrentLinks, subtitles
}

// AddMovie - Creates a movie
func (state *MovieManager) AddMovie(movie models.Movie) *models.MovieDb {
torrentLinks, subtitles := FindTorrentMagnetAndSubtitleLink(movie)

movieCreate := &models.MovieDb{
Type: movie.Type,
Expand Down Expand Up @@ -162,3 +166,7 @@ func (state *MovieManager) SearchMovies(title string) []models.MovieDb {
state.db.Where("LOWER(title) LIKE ?", "%"+title+"%").Find(&movies)
return movies
}

func (state *MovieManager) UpdateMovie(movie *models.MovieDb) {
state.db.Save(movie)
}
1 change: 1 addition & 0 deletions scrape-movie-service/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func CreateRoutes(api *api.API) *mux.Router {
u.HandleFunc("/getByImdbId/{imdbId}", api.GetMovieByImdbId).Methods("GET")
u.HandleFunc("/watchMagnet", api.WatchMagnet).Methods("POST")
u.HandleFunc("/searchMovies", api.SearchMovies).Methods("GET")
u.HandleFunc("/updateMovie/{movieId}", api.UpdateMovie).Methods("PUT")

return muxRouter
}
43 changes: 43 additions & 0 deletions scrape-movie-service/service/getMovieByTitle.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,46 @@ func FindByTitle(movieName string, movieYear string) models.Movie {

return MovieVar
}

func FindByImdbId(imdbId string) models.Movie {
var req string

req = "http://www.omdbapi.com/?apikey=314a1e4f&i=" + imdbId + "&type=movie&plot=full&r=json"
r, errGet := http.Get(req)
if errGet != nil {
req = "http://www.omdbapi.com/?apikey=8968bc83&i=" + imdbId + "&type=movie&plot=full&r=json"
}

defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
fmt.Printf("Error during closing body")
}
}(r.Body)

dec := json.NewDecoder(r.Body)
err := dec.Decode(&MovieVar)
if err != nil {
fmt.Printf("Error during json decoding Movie struct")
}

fmt.Printf("\n")

if MovieVar.Response == "True" {
fmt.Printf("%s (%s)\n", MovieVar.Title, MovieVar.Year)
fmt.Printf("%s | %s | %s | %s (%s)\n", MovieVar.Rated, MovieVar.Runtime, MovieVar.Genre, MovieVar.Released, MovieVar.Country)
fmt.Printf("Ratings: %s/10 from %s votes. \t Metascore: %s/100\n", MovieVar.ImdbRating, MovieVar.ImdbVotes, MovieVar.Metascore)
fmt.Printf("\n%s\n", MovieVar.Plot)
fmt.Printf("\nDirector: %s\n", MovieVar.Director)
fmt.Printf("Writer: %s\n", MovieVar.Writer)
fmt.Printf("Stars: %s\n", MovieVar.Actors)
fmt.Printf("\nPoster: %s\n", MovieVar.Poster)
fmt.Printf("\nImdb Page: http://www.imdb.com/title/%s\n", MovieVar.ImdbID)
} else {
fmt.Printf("%s\n", MovieVar.Error)
}

fmt.Printf("\n")

return MovieVar
}
34 changes: 34 additions & 0 deletions user-service/api/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,37 @@ func (api *API) GetUsersPage(w http.ResponseWriter, request *http.Request) {
return
}
}

func (api *API) UpdateUser(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")

params := mux.Vars(req)
userId, _ := strconv.ParseUint(params["userId"], 10, 64)

if !api.users.HasUserId(userId) {
http.Error(w, "User does not exist", http.StatusBadRequest)
return
}

decoder := json.NewDecoder(req.Body)
jsonData := models.UserUpdateDTO{}
err := decoder.Decode(&jsonData)

if err != nil {
http.Error(w, "Update data is wrong", http.StatusBadRequest)
return
}

user := api.users.FindUserByID(userId)
if jsonData.Age > 14 {
user.Age = jsonData.Age
}
if jsonData.Gender == "male" || jsonData.Gender == "female" {
user.Gender = jsonData.Gender
}
if len(jsonData.FavouriteTags) > 3 {
user.FavouriteTags = jsonData.FavouriteTags
}

api.users.UpdateUser(user)
}
14 changes: 14 additions & 0 deletions user-service/models/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ type UserDTO struct {
FavouriteTags string `json:"favouriteTags"`
}

type UserUpdateDTO struct {
Gender string `json:"gender"`
Age uint `json:"age"`
FavouriteTags string `json:"favouriteTags"`
}

type UsersPage struct {
Users *[]UserDTO `json:"users"`
// Page
Expand Down Expand Up @@ -113,6 +119,14 @@ func (state *UserManager) HasUser(username string) bool {
return true
}

// HasUserId - User with this id exists?
func (state *UserManager) HasUserId(id uint64) bool {
if err := state.db.Where("id=?", id).Find(&User{}).Error; err != nil {
return false
}
return true
}

// HashPassword - Hash the password
func (state *UserManager) HashPassword(password string) string {
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
Expand Down
1 change: 1 addition & 0 deletions user-service/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func CreateRoutes(api *api.API) *mux.Router {
u.HandleFunc("/username/{username}", api.GetUserByUsername).Methods("GET")
u.HandleFunc("/id/{id}", api.GetUserById).Methods("GET")
u.HandleFunc("/getUsersPage", api.GetUsersPage).Methods("GET")
u.HandleFunc("/updateUser/{userId}", api.UpdateUser).Methods("PUT")
u.Handle("/info", negroni.New(
negroni.HandlerFunc(auth.JwtMiddleware.HandlerWithNext),
negroni.Wrap(http.HandlerFunc(api.UserInfo)),
Expand Down

0 comments on commit 03e9af5

Please sign in to comment.