-
Notifications
You must be signed in to change notification settings - Fork 0
/
friend.go
73 lines (64 loc) · 1.75 KB
/
friend.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package models
import (
u "DummyMessengerAPI/utils"
"fmt"
"github.com/jinzhu/gorm"
)
type Friend struct {
gorm.Model
OwnerId uint `json:"owner_id"`
OwnId uint `json:"own_id"`
}
func (friend *Friend) Validate() (map[string]interface{}, bool) {
if friend.OwnId <= 0 {
return u.Message(false, "Non-existing user"), false
}
if friend.OwnerId <= 0 {
return u.Message(false, "User is not recognized"), false
}
if friend.OwnerId == friend.OwnId {
return u.Message(false, "Attempt to add yourself as user"), false
}
if AreFriends(friend.OwnId, friend.OwnerId) {
return u.Message(false, "Users are already friends"), false
}
cnt := 0
err := GetDB().Table("users").Where("id = ?", friend.OwnId).Count(&cnt).Error
if err != nil {
fmt.Println(err)
}
if cnt == 0 {
return u.Message(false, "Attempt to add nonexistent user"), false
}
return u.Message(true, "Requirement passed"), true
}
func (friend *Friend) Add() map[string]interface{} {
if resp, ok := friend.Validate(); !ok {
return resp
}
GetDB().Create(friend)
return u.Message(true, "Friend has been added")
}
func (friend *Friend) Remove() map[string]interface{} {
GetDB().Delete(friend)
return u.Message(true, "Friend has been removed")
}
func AreFriends(ownId, ownerId uint) bool {
cnt := 0
GetDB().Table("friends").Where("owner_id = ? and own_id = ? and deleted_at is null", ownerId, ownId).Count(&cnt)
return cnt > 0
}
func GetFriends(userId uint) []*User {
friendsId := make([]*Friend, 0)
err := GetDB().Table("friends").Where("owner_id = ?", userId).Find(&friendsId).Error
if err != nil {
fmt.Println(err)
return nil
}
friends := make([]*User, len(friendsId))
for i, curFriend := range friendsId {
tmp := getUser(curFriend.OwnId)
friends[i] = tmp
}
return friends
}