before
Go
package router
import (
"IM_for_imageProcess/api-01/utitl"
"encoding/json"
//"errors"
"fmt"
"net/http"
"time"
)
type History struct {
}
//var senderIdd int
//var receiverIdd int
//var messageContent string
//var createAt time.Time
//var isReceived string
type HistoryRes struct{
SenderIdd int `json:"sender_idd"`
ReceiverIdd int `json:"receiver_idd"`
MessageContent string `json:"message_content"`
CreateAt time.Time `json:"create_at"`
IsReceived string `json:"is_received"`
}
var HistoryResList []HistoryRes
func reverse(arr []HistoryRes) {
i:=0
j:=len(arr)-1
for{
if i < j{
arr[i],arr[j]=arr[j],arr[i]
i=i+1
j=j-1
} else{
break
}
}
}
func (this *History) GetHistoryMessage(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
return
}
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Credentials", "true")
requestMap := r.Form
senderId := requestMap["senderId"][0]
receiverId := requestMap["receiverId"][0]
//var HistoryResList []HistoryRes
//Id | int(11) | NO | PRI | NULL | auto_increment |
//| senderId | int(11) | YES | | NULL | |
//| receiverId | int(11) | YES | | NULL | |
//| messageContent | text | YES | | NULL | |
//| createAt | datetime | YES | | NULL | |
//| isReceived | varchar(3) | YES | | yes | |
//rows, err := utitl.Db.Query("select senderId,receiverId,messageContent,createAt,isReceived from MessageTable where senderId = ? and receiverId=? or receiverId=? and senderId=? order by createAt desc limit 15", senderId,receiverId,receiverId,senderId)
rows, err := utitl.Db.Query("select senderId,receiverId,messageContent,createAt,isReceived from MessageTable where senderId = ? and receiverId=? or receiverId=? and senderId=? order by createAt desc limit 15", senderId,receiverId,senderId,receiverId)
if err != nil {
fmt.Println(fmt.Sprintf("select tweetId from Tweet error: %v\n", err))
//return nil,errors.New(fmt.Sprintf("select tweetId from Tweet error: %v\n", err))
}
var senderIdd int
var receiverIdd int
var messageContent string
var createAt time.Time
var isReceived string
for rows.Next() {
err := rows.Scan(&senderIdd,&receiverIdd, &messageContent, &createAt,&isReceived)
if err != nil {
fmt.Printf("scan failed, err:%v\n", err)
//return nil,nil ,err
//return nil, err
//return nil,err
}
historyRes:=HistoryRes{
SenderIdd:senderIdd,
ReceiverIdd:receiverIdd,
MessageContent:messageContent,
CreateAt:createAt,
IsReceived:isReceived,
}
//tweetDetail := model.TweetDetail{
// TweetId: tweetId,
// UserId: star,
// Content: content,
// CreateAt: createAt,
// PostTime:postTime,
//}
//哈哈 这个就是我一个朋友的推文
//common.MapTweetDetail[createAt]=tweetDetail
HistoryResList=append(HistoryResList,historyRes)
//fmt.Println("tweetDetail=====",tweetDetail)
}
fmt.Println("len(HistoryResList)===",len(HistoryResList))
reverse(HistoryResList)
//sort.Stable(sort.Reverse(sort.IntSlice(HistoryResList)))
HistoryResListMar,err:=json.Marshal(HistoryResList)
//timeElapse:=time.Since(timeStart)
//fmt.Println("timeElapse=====",timeElapse)
//timeElapse=rpc获取friendsList +从tweettable 中获取明星的信息 +从news_feed表中获取普通人的信息+对两个表的信息排序
fmt.Fprintf(w,string(HistoryResListMar))
}
after:
after:
Go
package router
import (
"IM_for_imageProcess/api-01/utitl"
"encoding/json"
//"errors"
"fmt"
"net/http"
"time"
)
type History struct {
}
//var senderIdd int
//var receiverIdd int
//var messageContent string
//var createAt time.Time
//var isReceived string
type HistoryRes struct{
SenderIdd int `json:"sender_idd"`
ReceiverIdd int `json:"receiver_idd"`
MessageContent string `json:"message_content"`
CreateAt time.Time `json:"create_at"`
IsReceived string `json:"is_received"`
}
//var HistoryResList []HistoryRes
func reverse(arr []HistoryRes) {
i:=0
j:=len(arr)-1
for{
if i < j{
arr[i],arr[j]=arr[j],arr[i]
i=i+1
j=j-1
} else{
break
}
}
}
func (this *History) GetHistoryMessage(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
return
}
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Credentials", "true")
requestMap := r.Form
senderId := requestMap["senderId"][0]
receiverId := requestMap["receiverId"][0]
var HistoryResList []HistoryRes
//Id | int(11) | NO | PRI | NULL | auto_increment |
//| senderId | int(11) | YES | | NULL | |
//| receiverId | int(11) | YES | | NULL | |
//| messageContent | text | YES | | NULL | |
//| createAt | datetime | YES | | NULL | |
//| isReceived | varchar(3) | YES | | yes | |
//rows, err := utitl.Db.Query("select senderId,receiverId,messageContent,createAt,isReceived from MessageTable where senderId = ? and receiverId=? or receiverId=? and senderId=? order by createAt desc limit 15", senderId,receiverId,receiverId,senderId)
rows, err := utitl.Db.Query("select senderId,receiverId,messageContent,createAt,isReceived from MessageTable where senderId = ? and receiverId=? or receiverId=? and senderId=? order by createAt desc limit 15", senderId,receiverId,senderId,receiverId)
if err != nil {
fmt.Println(fmt.Sprintf("select tweetId from Tweet error: %v\n", err))
//return nil,errors.New(fmt.Sprintf("select tweetId from Tweet error: %v\n", err))
}
var senderIdd int
var receiverIdd int
var messageContent string
var createAt time.Time
var isReceived string
for rows.Next() {
err := rows.Scan(&senderIdd,&receiverIdd, &messageContent, &createAt,&isReceived)
if err != nil {
fmt.Printf("scan failed, err:%v\n", err)
//return nil,nil ,err
//return nil, err
//return nil,err
}
historyRes:=HistoryRes{
SenderIdd:senderIdd,
ReceiverIdd:receiverIdd,
MessageContent:messageContent,
CreateAt:createAt,
IsReceived:isReceived,
}
//tweetDetail := model.TweetDetail{
// TweetId: tweetId,
// UserId: star,
// Content: content,
// CreateAt: createAt,
// PostTime:postTime,
//}
//哈哈 这个就是我一个朋友的推文
//common.MapTweetDetail[createAt]=tweetDetail
HistoryResList=append(HistoryResList,historyRes)
//fmt.Println("tweetDetail=====",tweetDetail)
}
fmt.Println("len(HistoryResList)===",len(HistoryResList))
reverse(HistoryResList)
//sort.Stable(sort.Reverse(sort.IntSlice(HistoryResList)))
HistoryResListMar,err:=json.Marshal(HistoryResList)
//timeElapse:=time.Since(timeStart)
//fmt.Println("timeElapse=====",timeElapse)
//timeElapse=rpc获取friendsList +从tweettable 中获取明星的信息 +从news_feed表中获取普通人的信息+对两个表的信息排序
fmt.Fprintf(w,string(HistoryResListMar))
}