Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "database/sql"
- "encoding/json"
- "fmt"
- "os"
- _ "github.com/go-sql-driver/mysql"
- )
- //connect to database
- func dbConn() (db *sql.DB) {
- dbDriver := "mysql"
- dbUser := "root"
- dbPass := "root"
- dbName := "upwork"
- db, err := sql.Open(dbDriver, dbUser+":"+dbPass+"@/"+dbName)
- if err != nil {
- panic(err.Error())
- }
- return db
- }
- type topicItem struct {
- Id int `json:"id"`
- FileExist bool `json:"file_exist`
- ItemPiLoc string `json:"item_pi_loc"`
- }
- type Result struct {
- Status bool `json:"status"`
- Response interface{} `json:"response"`
- Error string `json:"error"`
- }
- func getVideoDetail(data []int) (Result, error) {
- var id int
- var itemPiLoc string
- db := dbConn()
- // we need define field list when run select query as will need for scan method
- sqlStatement := `SELECT id,item_pi_loc FROM TopicItems WHERE item_id =? and item_type_id= 1 order by item_order;`
- row := db.QueryRow(sqlStatement, data[1])
- result := Result{}
- switch err := row.Scan(&id, &itemPiLoc); err {
- case nil:
- fileName := "..%sCourseResource%s%d%svideos%s%d%s%s"
- dirPath := string(os.PathSeparator)
- fileName = fmt.Sprintf(fileName, dirPath, dirPath, data[0], dirPath, dirPath, data[1], dirPath, itemPiLoc)
- topic := topicItem{Id: id, ItemPiLoc: itemPiLoc}
- if _, err := os.Stat(fileName); err == nil {
- topic.FileExist = true
- }
- result.Status = true
- result.Response = topic
- default:
- result.Response = nil
- result.Error = err.Error()
- }
- return result, nil
- }
- func main() {
- data := []int{2, 1}
- result, _ := getVideoDetail(data)
- response, _ := json.Marshal(result)
- fmt.Println(string(response))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement