Exemplo n.º 1
0
func (i *MysqlSeriesRepository) GetAllSeries() ([]Series, error) {
	itemLst := make([]Series, 0)
	query := "select id, name, synopsis from series"
	rows, err := i.db.Query(query)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	defer rows.Close()
	for rows.Next() {
		var id string
		var name string
		var synopsis sql.NullString
		err := rows.Scan(&id, &name, &synopsis)
		if err != nil {
			log.Println(err)
			return nil, err
		}
		itemLst = append(itemLst, &ConcreteSeries{BaseItem: item.NewBaseItem(name, "", id), synopsis: synopsis.String})
	}
	err = rows.Err()
	if err != nil {
		return nil, err
	}

	return itemLst, nil
}
Exemplo n.º 2
0
func (i *MysqlSeriesRepository) NewSeries(name string) (Series, error) {
	//check if the series already exists.
	if ser, err := i.GetSeriesByName(name); err == nil {
		return ser, err
	}

	u, err := uuid.NewV4()
	newSeries := ConcreteSeries{BaseItem: item.NewBaseItem(name, "", u.String())}

	stmt, err := i.db.Prepare("INSERT INTO series(id,name) VALUES (?,?)")
	if err != nil {
		return nil, err
	}
	_, err = stmt.Exec(u.String(), name)
	if err != nil {
		if driverErr, ok := err.(*mysql.MySQLError); ok {
			switch {
			case driverErr.Number == 1062: // Item already existrs: http://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html#error_er_dup_entry
				//TODO: Error check
				s, _ := i.GetSeriesByName(name)
				return s, item.ErrAlreadyExists
			default:
				return nil, err
			}
		}
	}
	return &newSeries, nil
}
Exemplo n.º 3
0
func (i *MysqlSeriesRepository) GetSeriesByName(n string) (Series, error) {
	var id string
	var name string
	query := "select id, name from series where name = ?"

	err := i.db.QueryRow(query, n).Scan(&id, &name)
	if err != nil {
		if err == sql.ErrNoRows {
			return nil, item.ErrDoesNotExist
		} else {
			log.Fatal(err)
		}
	}
	return &ConcreteSeries{BaseItem: item.NewBaseItem(name, "", id)}, err
}
Exemplo n.º 4
0
func (i *MysqlSeriesRepository) GetSeriesById(id string) (Series, error) {
	var name string
	var synopsis sql.NullString
	var jname sql.NullString
	query := "select id, name, synopsis, jname from series where id = ?"
	err := i.db.QueryRow(query, id).Scan(&id, &name, &synopsis, &jname)
	if err != nil {
		if err == sql.ErrNoRows {
			return nil, item.ErrDoesNotExist
		} else {
			log.Fatal(err)
		}
	}

	s := ConcreteSeries{BaseItem: item.NewBaseItem(name, "", id)}
	s.SetSynopsis(synopsis.String)
	s.SetJName(jname.String)
	i.GetSeriesExtIds(&s)
	return &s, err
}
Exemplo n.º 5
0
func (i *MysqlSeriesRepository) GetSeriesByTag(item.Tag) ([]Series, error) {
	itemLst := make([]Series, 0)
	query := "select s.id, s.name from series s join seriesTag st on s.id=st.series_id join seriesTagName stn on st.tagName_id=stn.id and stn.name='featured'"
	rows, err := i.db.Query(query)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	for rows.Next() {
		var id string
		var name string
		err := rows.Scan(&id, &name)
		if err != nil {
			return nil, err
		}
		itemLst = append(itemLst, &ConcreteSeries{BaseItem: item.NewBaseItem(name, "", id)})
	}
	err = rows.Err()
	if err != nil {
		return nil, err
	}
	return itemLst, nil
}
Exemplo n.º 6
0
func (i *MemSeriesRepository) NewSeries(name string, species string) (Series, error) {
	newSeries := ConcreteSeries{BaseItem: item.NewBaseItem(name, species, string(idCount))}
	series[string(idCount)] = newSeries
	idCount += 1
	return &newSeries, nil
}
Exemplo n.º 7
0
//TODO: Add error checking
func NewConceteSeries(name, species, synopis, id, jn, syn string) (ConcreteSeries, error) {
	return ConcreteSeries{item.NewBaseItem(name, species, id), jn, syn, nil, nil}, nil
}