models.go 2.04 KB
Newer Older
zauberstuhl's avatar
zauberstuhl committed
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
/*
 * Unmappd - Visualize your Untappd checkins
 * Copyright (C) 2019 Lukas Matt <lukas@matt.wf>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package main

import (
  "time"
  "html/template"
  "strings"
  "fmt"

  "github.com/jinzhu/gorm"
  _ "github.com/jinzhu/gorm/dialects/sqlite"
)

type User struct {
  ID uint `gorm:"primary_key"`
  CreatedAt time.Time
  UpdatedAt time.Time

  Username string `gorm:"unique_index"`
zauberstuhl's avatar
zauberstuhl committed
37
  Token string `gorm:"index"`
zauberstuhl's avatar
zauberstuhl committed
38

39 40 41 42 43 44 45 46
  TotalBadges int
  TotalFriends int
  TotalCheckins int
  TotalBeers int
  TotalCreatedBeers int
  TotalFollowings int
  TotalPhotos int

zauberstuhl's avatar
zauberstuhl committed
47 48 49 50 51 52 53 54 55 56 57 58 59
  Locations Locations
}

type Users []User

func (user *User) AfterFind(db *gorm.DB) error {
  return db.Model(user).Related(&user.Locations).Error
}

type Location struct {
  ID uint `gorm:"primary_key"`
  CreatedAt time.Time

zauberstuhl's avatar
zauberstuhl committed
60
  UserID uint `gorm:"index"`
zauberstuhl's avatar
zauberstuhl committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

  Lat, Lng float64
  Url, Name, Category, Icon string
}

type Locations []Location

func (locations Locations) LatLngTimeJSON() template.JS {
  var result string
  for _, location := range locations {
    result += fmt.Sprintf(
      "{lng:%f,lat:%f,time:%d},",
      location.Lng, location.Lat, location.CreatedAt.Unix())
  }
  // trim last comma
  result = strings.TrimRight(result, ",")
  result = fmt.Sprintf("[%s]", result)
  return template.JS(result)
}

func openDatabase() (*gorm.DB, error) {
  db, err := gorm.Open("sqlite3", "unmappd.db")
  if err != nil {
    return db, err
  }
  db.LogMode(true)
  return db, err
}