This repository has been archived on 2025-03-01. You can view files and clone it, but cannot push or open issues or pull requests.
trantor/database.go

243 lines
4.9 KiB
Go
Raw Normal View History

2012-07-27 16:03:09 +02:00
package main
2012-08-19 02:29:34 +02:00
import (
"crypto/md5"
2012-08-19 02:29:34 +02:00
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
2013-03-20 00:54:40 +01:00
"time"
)
var db *DB
2012-07-27 16:03:09 +02:00
type Book struct {
2012-08-19 02:29:34 +02:00
Id string `bson:"_id"`
2012-07-30 23:23:38 +02:00
Title string
Author []string
Contributor string
Publisher string
Description string
Subject []string
Date string
Lang []string
2013-04-08 01:41:46 +02:00
Isbn string
2012-07-30 23:23:38 +02:00
Type string
Format string
Source string
Relation string
Coverage string
Rights string
Meta string
2013-04-12 01:05:40 +02:00
File bson.ObjectId
2013-04-15 22:10:48 +02:00
Cover bson.ObjectId
CoverSmall bson.ObjectId
Active bool
2012-08-15 15:12:59 +02:00
Keywords []string
2012-07-27 16:03:09 +02:00
}
2012-08-19 02:29:34 +02:00
type News struct {
Date time.Time
Text string
}
type DB struct {
session *mgo.Session
2012-09-14 00:34:13 +02:00
books *mgo.Collection
user *mgo.Collection
news *mgo.Collection
2013-04-22 23:28:00 +02:00
stats *mgo.Collection
2013-06-01 02:34:11 +02:00
mr *MR
}
func initDB() *DB {
var err error
d := new(DB)
d.session, err = mgo.Dial(DB_IP)
if err != nil {
panic(err)
}
2013-03-18 00:52:51 +01:00
database := d.session.DB(DB_NAME)
d.books = database.C(BOOKS_COLL)
d.user = database.C(USERS_COLL)
d.news = database.C(NEWS_COLL)
2013-04-22 23:28:00 +02:00
d.stats = database.C(STATS_COLL)
2013-06-01 02:34:11 +02:00
d.mr = NewMR(database)
return d
}
func (d *DB) Close() {
d.session.Close()
}
func md5Pass(pass string) []byte {
h := md5.New()
hash := h.Sum(([]byte)(PASS_SALT + pass))
return hash
}
func (d *DB) SetPassword(user string, pass string) error {
hash := md5Pass(pass)
return d.user.Update(bson.M{"user": user}, bson.M{"$set": bson.M{"pass": hash}})
}
func (d *DB) UserValid(user string, pass string) bool {
hash := md5Pass(pass)
n, err := d.user.Find(bson.M{"user": user, "pass": hash}).Count()
if err != nil {
return false
}
return n != 0
}
2013-06-01 20:43:23 +02:00
func (d *DB) UserRole(user string) string {
type result struct {
Role string
}
res := result{}
err := d.user.Find(bson.M{"user": user}).One(&res)
if err != nil {
return ""
}
return res.Role
}
func (d *DB) AddNews(text string) error {
var news News
news.Text = text
news.Date = time.Now()
return d.news.Insert(news)
}
2013-07-18 11:42:46 +02:00
func (d *DB) GetNews(num int, days int) (news []News, err error) {
query := bson.M{}
if days != 0 {
duration := time.Duration(-24*days) * time.Hour
date := time.Now().Add(duration)
query = bson.M{"date": bson.M{"$gt": date}}
}
q := d.news.Find(query).Sort("-date").Limit(num)
err = q.All(&news)
return
}
2013-04-22 23:28:00 +02:00
func (d *DB) InsertStats(stats interface{}) error {
return d.stats.Insert(stats)
}
func (d *DB) InsertBook(book interface{}) error {
return d.books.Insert(book)
}
func (d *DB) RemoveBook(id bson.ObjectId) error {
return d.books.Remove(bson.M{"_id": id})
}
func (d *DB) UpdateBook(id bson.ObjectId, data interface{}) error {
return d.books.Update(bson.M{"_id": id}, bson.M{"$set": data})
}
2012-08-21 11:25:02 +02:00
/* optional parameters: length and start index
2013-04-01 14:05:04 +02:00
*
2012-08-21 11:25:02 +02:00
* Returns: list of books, number found and err
*/
func (d *DB) GetBooks(query bson.M, r ...int) (books []Book, num int, err error) {
2012-08-21 11:25:02 +02:00
var start, length int
if len(r) > 0 {
length = r[0]
if len(r) > 1 {
start = r[1]
}
}
q := d.books.Find(query).Sort("-_id")
2012-08-21 11:25:02 +02:00
num, err = q.Count()
if err != nil {
return
}
if start != 0 {
q = q.Skip(start)
}
if length != 0 {
q = q.Limit(length)
}
err = q.All(&books)
2012-08-19 02:29:34 +02:00
for i, b := range books {
books[i].Id = bson.ObjectId(b.Id).Hex()
}
2012-08-21 11:25:02 +02:00
return
}
/* Get the most visited books
*/
func (d *DB) GetVisitedBooks(num int) (books []Book, err error) {
bookId, err := d.mr.GetMostVisited(num, d.stats)
if err != nil {
return nil, err
}
books = make([]Book, num)
for i, id := range bookId {
d.books.Find(bson.M{"_id": id}).One(&books[i])
books[i].Id = bson.ObjectId(books[i].Id).Hex()
}
return
}
/* Get the most downloaded books
*/
func (d *DB) GetDownloadedBooks(num int) (books []Book, err error) {
bookId, err := d.mr.GetMostDownloaded(num, d.stats)
if err != nil {
return nil, err
}
books = make([]Book, num)
for i, id := range bookId {
d.books.Find(bson.M{"_id": id}).One(&books[i])
books[i].Id = bson.ObjectId(books[i].Id).Hex()
}
return
}
2012-10-31 10:24:17 +01:00
/* optional parameters: length and start index
2013-04-01 14:05:04 +02:00
*
2012-10-31 10:24:17 +01:00
* Returns: list of books, number found and err
*/
2012-10-31 10:24:17 +01:00
func (d *DB) GetNewBooks(r ...int) (books []Book, num int, err error) {
return d.GetBooks(bson.M{"$nor": []bson.M{{"active": true}}}, r...)
2012-08-19 02:29:34 +02:00
}
2012-08-21 11:35:06 +02:00
2012-09-12 21:47:43 +02:00
func (d *DB) BookActive(id bson.ObjectId) bool {
var book Book
err := d.books.Find(bson.M{"_id": id}).One(&book)
if err != nil {
return false
}
return book.Active
}
2013-04-12 01:05:40 +02:00
func (d *DB) GetFS(prefix string) *mgo.GridFS {
return d.session.DB(DB_NAME).GridFS(prefix)
}
2013-03-20 00:54:40 +01:00
func (d *DB) GetTags(numTags int) ([]string, error) {
2013-06-01 02:34:11 +02:00
return d.mr.GetTags(numTags, d.books)
2012-08-21 11:35:06 +02:00
}
2013-05-05 01:39:28 +02:00
2013-05-31 23:59:01 +02:00
type Visits struct {
Date int64 "_id"
Count int "value"
2013-05-05 01:39:28 +02:00
}
2013-06-01 03:40:06 +02:00
func (d *DB) GetHourVisits(start time.Time) ([]Visits, error) {
return d.mr.GetHourVisits(start, d.stats)
}
2013-05-31 23:59:01 +02:00
func (d *DB) GetDayVisits(start time.Time) ([]Visits, error) {
2013-06-01 02:34:11 +02:00
return d.mr.GetDayVisits(start, d.stats)
2013-05-05 01:39:28 +02:00
}
2013-06-01 03:40:06 +02:00
func (d *DB) GetMonthVisits(start time.Time) ([]Visits, error) {
return d.mr.GetMonthVisits(start, d.stats)
}