-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change each service to act like a transaction
- Loading branch information
1 parent
9aa2b82
commit 3a630db
Showing
9 changed files
with
179 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package handle | ||
|
||
import "database/sql" | ||
|
||
func Transaction(tx *sql.Tx, e error) (err error) { | ||
if err := tx.Rollback(); err != nil { | ||
return err | ||
} | ||
|
||
return e | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,116 @@ | ||
package rsschannel | ||
|
||
import ( | ||
"database/sql" | ||
"github.com/mmcdole/gofeed" | ||
"github.com/shhj1998/rss-search-api/rsserver/handle" | ||
"github.com/shhj1998/rss-search-api/rsserver/rssitem" | ||
) | ||
|
||
func (table *Table) GetLinks(channels *[]*Schema) (err error) { | ||
channelRows, err := table.Connection.Query(`SELECT channel_id, rss_link FROM Channel`) | ||
var tx *sql.Tx | ||
if tx, err = table.Connection.Begin(); err != nil { | ||
return err | ||
} | ||
|
||
channelRows, err := tx.Query(`SELECT channel_id, rss_link FROM Channel`) | ||
|
||
if err == nil { | ||
for channelRows.Next() { | ||
var link Schema | ||
if err := channelRows.Scan(&link.Id, &link.RSSLink); err != nil { | ||
return nil | ||
return handle.Transaction(tx, err) | ||
} | ||
|
||
*channels = append(*channels, &link) | ||
} | ||
} else { | ||
return err | ||
return handle.Transaction(tx, err) | ||
} | ||
|
||
return nil | ||
return tx.Commit() | ||
} | ||
|
||
func (table *Table) Get(channels *[]*Schema) (err error) { | ||
channelRows, err := table.Connection.Query(`SELECT title, description, site_link FROM Channel`) | ||
var tx *sql.Tx | ||
if tx, err = table.Connection.Begin(); err != nil { | ||
return err | ||
} | ||
|
||
channelRows, err := tx.Query(`SELECT channel_id, title, description, site_link, rss_link FROM Channel`) | ||
|
||
if err == nil { | ||
if err := Fetch(channelRows, channels); err != nil { | ||
return err | ||
return handle.Transaction(tx, err) | ||
} | ||
} else { | ||
return err | ||
return handle.Transaction(tx, err) | ||
} | ||
|
||
return nil | ||
return tx.Commit() | ||
} | ||
|
||
func (table *Table) getChannelWithItems(channel *Schema, count int) (err error) { | ||
var tx *sql.Tx | ||
var items []*rssitem.Schema | ||
itemRows, err := table.Connection.Query(`SELECT I.guid, I.title, I.link, I.description, I.pub_date, I.creator, e.url, e.length, e.type | ||
FROM Channel c JOIN Publish p ON c.channel_id=p.channel JOIN Item I ON p.item=I.guid LEFT JOIN Enclosure e ON I.guid=e.item | ||
|
||
if tx, err = table.Connection.Begin(); err != nil { | ||
return err | ||
} | ||
|
||
itemRows, err := tx.Query(`SELECT I.guid, I.title, I.link, I.description, I.pub_date, I.creator, e.url, e.length, e.type | ||
FROM Channel c JOIN Publish p ON c.channel_id=p.channel JOIN Item I ON p.item=I.item_id LEFT JOIN Enclosure e ON I.guid=e.item | ||
WHERE channel_id=? ORDER BY I.pub_date DESC LIMIT 0,?`, channel.Id, count) | ||
if err != nil { | ||
return err | ||
return handle.Transaction(tx, err) | ||
} else if err = rssitem.Fetch(itemRows, &items); err != nil { | ||
return err | ||
return handle.Transaction(tx, err) | ||
} else { | ||
for idx := range items { | ||
channel.Items = append(channel.Items, &items[idx].Item) | ||
} | ||
} | ||
|
||
return nil | ||
return tx.Commit() | ||
} | ||
|
||
func (table *Table) GetWithItems(channels *[]*Schema, count int) (err error) { | ||
channelRows, err := table.Connection.Query(`SELECT channel_id, title, description, site_link, rss_link FROM Channel`) | ||
|
||
if err != nil { | ||
var tx *sql.Tx | ||
if tx, err = table.Connection.Begin(); err != nil { | ||
return err | ||
} | ||
|
||
if channelRows, err := tx.Query(`SELECT channel_id, title, description, site_link, rss_link FROM Channel`); err != nil { | ||
return handle.Transaction(tx, err) | ||
} else { | ||
if err = Fetch(channelRows, channels); err != nil { | ||
return err | ||
return handle.Transaction(tx, err) | ||
} else { | ||
for idx := range *channels { | ||
if err = table.getChannelWithItems((*channels)[idx], count); err != nil { | ||
return err | ||
return handle.Transaction(tx, err) | ||
} | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
return tx.Commit() | ||
} | ||
|
||
func (table *Table) Create(rssLink string) (err error) { | ||
var tx *sql.Tx | ||
if tx, err = table.Connection.Begin(); err != nil { | ||
return err | ||
} | ||
|
||
fp := gofeed.NewParser() | ||
if _, err = fp.ParseURL(rssLink); err == nil { | ||
if _, err = table.Connection.Exec(`INSERT INTO Channel(rss_link) VALUE(?)`, rssLink); err != nil { | ||
return err | ||
if _, err = tx.Exec(`INSERT INTO Channel(rss_link) VALUE(?)`, rssLink); err != nil { | ||
return handle.Transaction(tx, err) | ||
} | ||
} else { | ||
return err | ||
return handle.Transaction(tx, err) | ||
} | ||
|
||
return nil | ||
return tx.Commit() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,25 @@ | ||
package rssitem | ||
|
||
import ( | ||
"database/sql" | ||
"github.com/shhj1998/rss-search-api/rsserver/handle" | ||
) | ||
|
||
func (table *Table) Get(items *[]*Schema) (err error) { | ||
itemRows, err := table.Connection.Query(`SELECT guid, title, link, description, pub_date, creator, url, length, type FROM Item LEFT JOIN Enclosure ON guid=item`) | ||
var tx *sql.Tx | ||
if tx, err = table.Connection.Begin(); err != nil { | ||
return err | ||
} | ||
|
||
itemRows, err := tx.Query(`SELECT guid, title, link, description, pub_date, creator, url, length, type FROM Item LEFT JOIN Enclosure ON item_id=item`) | ||
|
||
if err == nil { | ||
if err := Fetch(itemRows, items); err != nil { | ||
return err | ||
return handle.Transaction(tx, err) | ||
} | ||
} else { | ||
return err | ||
return handle.Transaction(tx, err) | ||
} | ||
|
||
return nil | ||
return tx.Commit() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.