Skip to content

Commit

Permalink
Resolved code inconsistency
Browse files Browse the repository at this point in the history
  • Loading branch information
HeartLinked committed May 24, 2024
1 parent 2c76b3c commit e3bb544
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 32 deletions.
11 changes: 0 additions & 11 deletions controllers/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"encoding/gob"

"github.com/beego/beego"
"github.com/casbin/caswaf/object"
"github.com/casdoor/casdoor-go-sdk/casdoorsdk"
)

Expand Down Expand Up @@ -48,16 +47,6 @@ func wrapActionResponse(affected bool, e ...error) *Response {
}
}

func wrapRecordResponse(record *object.Record, err error) *Response {
if err != nil {
return &Response{Status: "error", Msg: err.Error()}
}
if record != nil {
return &Response{Status: "ok", Data: record}
}
return &Response{Status: "error", Msg: "Record not found"}
}

func (c *ApiController) GetSessionClaims() *casdoorsdk.Claims {
s := c.GetSession("user")
if s == nil {
Expand Down
8 changes: 1 addition & 7 deletions controllers/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,6 @@ func (c *ApiController) AddRecord() {
return
}

addedRecord, err := object.AddRecord(&record)
if err != nil {
c.ResponseError(err.Error())
return
}

c.Data["json"] = wrapRecordResponse(addedRecord, err)
c.Data["json"] = wrapActionResponse(object.AddRecord(&record))
c.ServeJSON()
}
9 changes: 5 additions & 4 deletions object/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ func GetRecords(owner string) ([]*Record, error) {
return records, nil
}

func AddRecord(record *Record) (*Record, error) {
_, err := ormer.Engine.Insert(record)
func AddRecord(record *Record) (bool, error) {
affected, err := ormer.Engine.Insert(record)
if err != nil {
return nil, err
return false, err
}
return record, nil

return affected != 0, nil
}

func DeleteRecord(record *Record) (bool, error) {
Expand Down
10 changes: 3 additions & 7 deletions service/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func getHostNonWww(host string) string {
return res
}

func getClientIP(r *http.Request) string {
func getClientIp(r *http.Request) string {
forwarded := r.Header.Get("X-Forwarded-For")
if forwarded != "" {
clientIP := strings.Split(forwarded, ",")[0]
Expand Down Expand Up @@ -97,11 +97,7 @@ func logRequest(clientIp string, r *http.Request) {
ClientIp: clientIp,
UserAgent: r.UserAgent(),
}
fmt.Println(util.GetCurrentTime())
_, err := object.AddRecord(&record)
if err != nil {
fmt.Println(err.Error())
}
object.AddRecord(&record)
}
}

Expand All @@ -121,7 +117,7 @@ func redirectToHost(w http.ResponseWriter, r *http.Request, host string) {
}

func handleRequest(w http.ResponseWriter, r *http.Request) {
clientIp := getClientIP(r)
clientIp := getClientIp(r)
logRequest(clientIp, r)

site := getSiteByDomainWithWww(r.Host)
Expand Down
1 change: 1 addition & 0 deletions web/src/RecordListPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class RecordListPage extends BaseListPage {
this.setState({
data: Setting.addRow(this.state.data, res.data),
});
this.fetch();
}
}
)
Expand Down
3 changes: 0 additions & 3 deletions web/src/backend/RecordBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ export function addRecord(record) {
return fetch(`${Setting.ServerUrl}/api/add-record`, {
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(newRecord),
}).then(res => res.json());
}

0 comments on commit e3bb544

Please sign in to comment.