Exemplo n.º 1
0
func DeleteChirp(c *gin.Context) {
	var id = c.Params.ByName("id")
	var chirp model.Chirp

	uid, _ := c.Get("userid")
	userid, _ := Atoi(uid.(string))
	chirp.Id, _ = Atoi(id)

	row := util.DB.QueryRow(chirp.StmtGetById())
	err := row.Scan(&chirp.Id, &chirp.Userid, &chirp.Type, &chirp.Message, &chirp.Create_date, &chirp.Active)
	if err != nil {
		c.JSON(400, sys.JSON_NOACCESS)
		return
	}

	// Check if chirp belongs to requester's account
	if chirp.Userid != userid {
		c.JSON(403, sys.JSON_NOACCESS)
		return
	}

	row = util.DB.QueryRow(chirp.StmtSetActive(false))
	err = row.Scan(&chirp.Id)
	if err != nil {
		c.JSON(500, gin.H{"message": sys.ERR_DB_DELETE, "status": sys.FAIL, "payload": id})
		return
	}
	c.JSON(200, gin.H{"payload": "Deleted Chirp " + Itoa(chirp.Id), "status": sys.SUCCESS})

}
Exemplo n.º 2
0
func PostChirp(c *gin.Context) {
	var chirp model.Chirp
	body, err := ioutil.ReadAll(c.Request.Body)
	if err != nil {
		c.JSON(400, gin.H{"message": sys.ERR_READ_BODY, "status": sys.FAIL})
		return
	}

	err = json.Unmarshal(body, &chirp)
	if err != nil {
		c.JSON(500, gin.H{"message": sys.ERR_UNMARSHAL_BODY, "status": sys.FAIL, "payload": body})
		return
	}

	uid, _ := c.Get("userid")
	chirp.Userid, _ = Atoi(uid.(string))

	row := util.DB.QueryRow(chirp.StmtInsert())
	err = row.Scan(&chirp.Id)
	if err != nil {
		c.JSON(500, gin.H{"message": sys.ERR_DB_INSERT, "status": sys.FAIL, "payload": body})
		return
	}
	c.JSON(200, gin.H{"payload": chirp.Id, "status": sys.SUCCESS})
}
Exemplo n.º 3
0
func GetChirpsByUser(c *gin.Context) {
	var arr []model.Chirp
	var chirp model.Chirp

	uid, _ := c.Get("userid")
	chirp.Id, _ = Atoi(uid.(string))

	log.Printf("... GetUser ... ")
	rows, err := util.DB.Query(chirp.StmtSelectByUserId())
	if err != nil {
		c.JSON(400, gin.H{"message": sys.ERR_DB_NO_MATCH, "status": sys.FAIL})
		return
	}

	//util.PanicIf(err)
	for rows.Next() {
		var cp model.Chirp
		if err := rows.Scan(&cp.Id, &cp.Type, &cp.Message, &cp.Create_date, &cp.Active); err != nil {
			c.JSON(400, gin.H{"message": sys.ERR_SCAN_ROW, "status": sys.FAIL})
			return
		} else {
			arr = append(arr, cp)
		}
	}

	c.JSON(200, arr)
}
Exemplo n.º 4
0
func GetChirps(c *gin.Context) {
	var arr []model.Chirp
	var chirp model.Chirp

	rows, err := util.DB.Query(chirp.StmtSelectActive())
	if err != nil {
		c.JSON(400, gin.H{"message": sys.ERR_DB_NO_MATCH, "status": sys.FAIL})
		return
	}

	//util.PanicIf(err)
	for rows.Next() {
		var cp model.Chirp
		if err := rows.Scan(&cp.Id, &cp.Userid, &cp.Type, &cp.Message, &cp.Create_date, &cp.Active); err != nil {
			c.JSON(400, gin.H{"message": sys.ERR_SCAN_ROW, "status": sys.FAIL})
			return
		} else {
			arr = append(arr, cp)
		}
	}

	c.JSON(200, gin.H{"payload": arr, "status": sys.SUCCESS})
}
Exemplo n.º 5
0
func GetChirpById(c *gin.Context) {
	var id = c.Params.ByName("id")
	var chirp model.Chirp

	uid, _ := c.Get("userid")
	userid, _ := Atoi(uid.(string))
	chirp.Id, _ = Atoi(id)

	row := util.DB.QueryRow(chirp.StmtGetById())
	err := row.Scan(&chirp.Id, &chirp.Userid, &chirp.Type, &chirp.Message, &chirp.Create_date, &chirp.Active)
	if err != nil {
		c.JSON(400, sys.JSON_NOACCESS)
		return
	}

	// Check if chirp belongs to requester's account
	if chirp.Userid != userid {
		c.JSON(403, sys.JSON_NOACCESS)
		return
	}

	c.JSON(200, gin.H{"payload": chirp, "status": sys.SUCCESS})
}
Exemplo n.º 6
0
func PatchChirp(c *gin.Context) {
	var chirp model.Chirp
	body, err := ioutil.ReadAll(c.Request.Body)
	if err != nil {
		c.JSON(400, gin.H{"message": sys.ERR_READ_BODY, "status": sys.FAIL})
		return
	}
	err = json.Unmarshal(body, &chirp)
	if err != nil {
		c.JSON(500, gin.H{"message": sys.ERR_UNMARSHAL_BODY, "status": sys.FAIL, "payload": body})
		return
	}

	// TODO : Check if this Userid can access this payment
	row := util.DB.QueryRow(chirp.StmtUpdate())
	err = row.Scan(&chirp.Id)
	if err != nil {
		c.JSON(500, gin.H{"message": sys.ERR_DB_UPDATE, "status": sys.FAIL, "payload": body})
		return
	}
	log.Printf("======== type: " + chirp.Type)
	c.JSON(200, gin.H{"payload": "Updated Chirp " + Itoa(chirp.Id), "status": sys.SUCCESS})
}
Exemplo n.º 7
0
func Test_Chirp(t *testing.T) {

	cfg.Setup("../../../../cfg.json")
	util.SetupDB()

	var a model.Chirp
	var b model.Chirp
	var c model.Chirp
	var token = util.GenHash(6)
	//var id int = 110
	a.Userid = 1
	a.Type = token
	a.Message = token + "-message"

	log.Println(a)
	// Test StmtInsert
	row := util.DB.QueryRow(a.StmtInsert())
	err := row.Scan(&b.Id)
	log.Println(b)
	if err != nil {
		t.Error("FAILED QueryRow : ", b)
	}

	// Test StmtGetById
	row = util.DB.QueryRow(b.StmtGetById())
	err = row.Scan(&b.Id, &b.Userid, &b.Type, &b.Message, &b.Create_date, &b.Active)
	if err != nil {
		t.Error("FAILED Prepare Statement : ", err)
	}

	if a.Userid != b.Userid {
		t.Error("FAILED not same User : "******"FAILED not same Type : ", a.Type)
	}
	if a.Message != b.Message {
		t.Error("FAILED not same Message : ", a.Message)
	}

	// Test StmtUpdate
	b.Message = "updated-msg"
	b.Type = "updated"
	log.Println("b is now updated : ", b)

	_, err = util.DB.Exec(b.StmtUpdate())
	log.Println("Update query : ", b.StmtUpdate())
	log.Println("This, the error : ", err)

	c.Id = b.Id
	log.Println("Using this id: ", c.Id, b.Id)

	err = util.DB.QueryRow(c.StmtGetById()).Scan(&c.Id, &c.Userid, &c.Type, &c.Message, &c.Create_date, &c.Active)
	if err != nil {
		t.Error("FAILED scan row : ", err)
	}

	log.Println("C now looks like : ", c)

	if c.Type != "updated" {
		t.Error("FAILED update Name : ", c.Type)
	}
	if c.Message != "updated-msg" {
		t.Error("FAILED update Message : ", c.Message)
	}

	// Test StmtSetActive
	_, err = util.DB.Exec(c.StmtSetActive(false))
	if err != nil {
		t.Error("FAILED StmtSetActive to false : ", err)
	}

	err = util.DB.QueryRow(c.StmtGetById()).Scan(&c.Id, &c.Userid, &c.Type, &c.Message, &c.Create_date, &c.Active)
	if err != nil {
		t.Error("FAILED scan row : ", err)
	}

	if c.Active != false {
		t.Error("FAILED updated SetActive(false) : ", c.Active)
	}

	// Test StmtSetActive
	_, err = util.DB.Exec(c.StmtSetActive(true))
	if err != nil {
		t.Error("FAILED StmtSetActive to false : ", err)
	}

	// Test StmtSelect
	rows, err := util.DB.Query(b.StmtSelect(""))
	if err != nil {
		t.Error("FAILED StmtSelect : ", err)
	}

	for rows.Next() {
		rows.Scan(&b.Id, &b.Userid, &b.Type, &b.Message, &b.Create_date, &b.Active)
		log.Println("This is a unit row : ", b)
	}

	// Test StmtSelectActive
	rows, err = util.DB.Query(a.StmtSelectActive())
	if err != nil {
		t.Error("FAILED StmtSelectActive : ", err)
	}
	for rows.Next() {
		rows.Scan(&b.Id, &b.Userid, &b.Type, &b.Message, &b.Create_date, &b.Active)
		log.Println("This is an active row : ", b)
	}

	// Test StmtSelectActive
	rows, err = util.DB.Query(a.StmtSelectByUserId())
	if err != nil {
		t.Error("FAILED StmtSelectByUserId : ", err)
	}
	for rows.Next() {
		rows.Scan(&b.Id, &b.Userid, &b.Type, &b.Message, &b.Create_date, &b.Active)
		log.Println("This is has for unit row : ", b)
	}

	// Test StmtCountActive
	var cnt int
	err = util.DB.QueryRow(c.StmtCountActive(true)).Scan(&cnt)
	if err != nil {
		t.Error("FAILED StmtCountActive", err)
	}
	if cnt < 1 {
		t.Error("FAILED found no active Units : ", cnt)
	}

}