0 package db

1

2 import (

3 "log"

4 )

5

6 func (user User) GetSessionsCount() (int, error) {

7 row, err := db.Query("SELECT COUNT(userID) FROM certificate " +

8 "WHERE userID = ?", user.ID)

9 if err != nil {

10 return -1, err

11 }

12 defer row.Close()

13 count := 0

14 if row.Next() {

15 err = row.Scan(&count)

16 if err != nil {

17 return -1, err

18 }

19 }

20 return count, nil

21 }

22

23 func (user User) Disconnect(signature string) error {

24 if err := user.VerifySignature(signature); err != nil {

25 return err

26 }

27 delete(users, signature)

28 _, err := db.Exec("DELETE FROM certificate WHERE hash = ?", signature)

29 if err != nil {

30 return err

31 }

32 return nil

33 }

34

35 func (user User) DisconnectAll(signature string) error {

36 if err := user.VerifySignature(signature); err != nil {

37 return err

38 }

39 rows, err := db.Query("SELECT hash FROM certificate WHERE userID = ?",

40 user.ID)

41 if err != nil {

42 return err

43 }

44 defer rows.Close()

45 for rows.Next() {

46 var hash string

47 err = rows.Scan(&hash)

48 if err != nil {

49 return err

50 }

51 if hash != signature {

52 delete(users, hash)

53 }

54 }

55 _, err = db.Exec("DELETE FROM certificate WHERE userID = ? AND " +

56 "hash <> ?", user.ID, signature)

57 if err != nil {

58 return err

59 }

60 return nil

61 }

62

63 func (user User) CreateSession(signature string) error {

64 _, err := db.Exec("INSERT INTO certificate (userID, hash, creation) " +

65 "VALUES (?, ?, " + unixTime + ")", user.ID, signature)

66 if err != nil {

67 log.Println(err.Error())

68 return err

69 }

70 user.Signature = signature

71 users[signature] = user

72 return nil

73 }

74