controllers

package
v0.0.0-...-bd6b2d5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 2, 2020 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AddFriend = func(w http.ResponseWriter, r *http.Request) {
	userId := r.Context().Value("user_id").(uint)
	friend := &models.Friend{}
	err := json.NewDecoder(r.Body).Decode(friend)
	if err != nil {
		u.Response(w, http.StatusForbidden, u.Message(false, "Incorrect request"))
		return
	}

	friend.OwnerId = userId
	friendRev := &models.Friend{OwnId: friend.OwnerId, OwnerId: friend.OwnId}
	friendRev.Add()
	u.Response(w, http.StatusOK, friend.Add())
}
View Source
var Authorize = func(w http.ResponseWriter, r *http.Request) {
	user := &models.User{}
	err := json.NewDecoder(r.Body).Decode(user)
	if err != nil {
		u.Response(w, http.StatusForbidden, u.Message(false, "Invalid request"))
		return
	}
	u.Response(w, http.StatusOK, models.Login(user.Login, user.Password))
}
View Source
var CreateUser = func(w http.ResponseWriter, r *http.Request) {
	user := &models.User{}
	err := json.NewDecoder(r.Body).Decode(user)
	if err != nil {
		u.Response(w, http.StatusForbidden, u.Message(false, "Invalid request"))
		return
	}
	u.Response(w, http.StatusOK, user.Create())
}
View Source
var GetFriendsFor = func(w http.ResponseWriter, r *http.Request) {
	userId := r.Context().Value("user_id").(uint)
	friends := models.GetFriends(userId)
	resp := u.Message(true, "Friend list generated")
	resp["friends"] = friends
	u.Response(w, http.StatusOK, resp)
}
View Source
var GetLastMessage = func(w http.ResponseWriter, r *http.Request) {
	message := &models.Message{}
	userId := r.Context().Value("user_id").(uint)
	err := json.NewDecoder(r.Body).Decode(message)
	if err != nil {
		u.Response(w, http.StatusForbidden, u.Message(false, "Incorrect request"))
		return
	}

	message = models.GetLastMessage(userId, message.ReceiverId)

	resp := u.Message(true, "Last message generated")
	resp["message"] = message
	u.Response(w, http.StatusOK, resp)
}
View Source
var GetMessages = func(w http.ResponseWriter, r *http.Request) {
	message := &models.Message{}
	userId := r.Context().Value("user_id").(uint)
	err := json.NewDecoder(r.Body).Decode(message)
	if err != nil {
		u.Response(w, http.StatusForbidden, u.Message(false, "Incorrect request"))
		return
	}

	messages := models.GetMessages(userId, message.ReceiverId)

	resp := u.Message(true, "Messages list generated")
	resp["messages"] = messages
	u.Response(w, http.StatusOK, resp)
}
View Source
var Logout = func(w http.ResponseWriter, r *http.Request) {
	token, _ := jwt.Parse(strings.Split(r.Header.Get("Authorization"), " ")[1],
		func(token *jwt.Token) (interface{}, error) {
			return []byte(os.Getenv("token_password")), nil
		})
	u.Response(w, http.StatusOK, models.Logout(token.Raw))
}
View Source
var RemoveFriend = func(w http.ResponseWriter, r *http.Request) {
	userId := r.Context().Value("user_id").(uint)
	friend := &models.Friend{}
	err := json.NewDecoder(r.Body).Decode(friend)
	if err != nil {
		u.Response(w, http.StatusForbidden, u.Message(false, "Incorrect request"))
		return
	}

	friend.OwnerId = userId
	friendRev := &models.Friend{OwnId: friend.OwnerId, OwnerId: friend.OwnId}
	friendRev.Remove()
	u.Response(w, http.StatusOK, friend.Remove())
}
View Source
var SendMessage = func(w http.ResponseWriter, r *http.Request) {
	message := &models.Message{}
	userId := r.Context().Value("user_id").(uint)
	err := json.NewDecoder(r.Body).Decode(message)
	if err != nil {
		u.Response(w, http.StatusForbidden, u.Message(false, "Incorrect request"))
		return
	}

	message.SenderId = userId

	if !models.AreFriends(message.SenderId, message.ReceiverId) {
		friend := &models.Friend{OwnId: message.ReceiverId, OwnerId: message.SenderId}
		friendRev := &models.Friend{OwnId: friend.OwnerId, OwnerId: friend.OwnId}
		friend.Add()
		friendRev.Add()
	}

	u.Response(w, http.StatusOK, message.Create())
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL