Skip to content

Commit

Permalink
Merge pull request #29 from je4/dev
Browse files Browse the repository at this point in the history
set source added
  • Loading branch information
je4 authored Nov 18, 2021
2 parents 77f46fa + 6600d6c commit d00127d
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
29 changes: 29 additions & 0 deletions pkg/fair/fair.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,35 @@ func (f *Fair) GetSourceByName(name string, partitionName string) (*Source, erro
return nil, errors.New(fmt.Sprintf("source %s for partition %s not found", name, partitionName))
}

func (f *Fair) SetSource(src *Source) error {
sqlstr := fmt.Sprintf("SELECT sourceid FROM %s.source WHERE name=$1", f.dbSchema)
var sourceId int64
if err := f.db.QueryRow(sqlstr, src.Name).Scan(&sourceId); err != nil {
if err != sql.ErrNoRows {
return errors.Wrapf(err, "cannot query database - %s [%v]", sqlstr, src.ID)
}
}
if sourceId > 0 {
sqlstr = fmt.Sprintf("UPDATE %s.source "+
"SET name=$1, detailurl=$2, description=$3, oai_domain=$4, partition=$5 WHERE sourceid=$6 ", f.dbSchema)
values := []interface{}{src.Name, src.DetailURL, src.Description, src.OAIDomain, src.Partition, sourceId}
if _, err := f.db.Exec(sqlstr, values...); err != nil {
return errors.Wrapf(err, "cannot insert into source database - %s [%v]", sqlstr, values)
}
} else {
sqlstr = fmt.Sprintf("INSERT INTO %s.source (name, detailurl, description, oai_domain, partition) "+
"VALUES($1, $2, $3, $4, $5)", f.dbSchema)
values := []interface{}{src.Name, src.DetailURL, src.Description, src.OAIDomain, src.Partition}
if _, err := f.db.Exec(sqlstr, values...); err != nil {
return errors.Wrapf(err, "cannot update source database - %s [%v]", sqlstr, values)
}
}
if err := f.LoadSources(); err != nil {
return errors.Wrap(err, "cannot load sources")
}
return nil
}

func (f *Fair) GetSourceByOAIDomain(name string) (*Source, error) {
f.sourcesMutex.RLock()
defer f.sourcesMutex.RUnlock()
Expand Down
38 changes: 38 additions & 0 deletions pkg/fairclient/FairClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,44 @@ func (fs *FairClient) Create(item *fair.ItemData) (*fair.ItemData, error) {
return result.Item, nil
}

func (fs *FairClient) SetSource(src *fair.Source) error {
tr, err := JWTInterceptor.NewJWTTransport(
fs.service,
"setSource",
JWTInterceptor.Secure,
nil,
sha512.New(),
fs.jwtKey,
fs.jwtAlg,
fs.jwtLifetime)
if err != nil {
return errors.Wrapf(err, "cannot create jwt transport")
}
client := http.Client{Transport: tr}

data, err := json.Marshal(src)
if err != nil {
return errors.Wrapf(err, "cannot marshal [%v]", src)
}

response, err := client.Post(fs.address+"/source", "application/json", bytes.NewBuffer(data))
if err != nil {
return errors.Wrapf(err, "cannot post to %s", fs.address)
}
bodyBytes, err := ioutil.ReadAll(response.Body)
if err != nil {
return errors.Wrap(err, "cannot read response body")
}
result := service.CreateResultStatus{}
if err := json.Unmarshal(bodyBytes, &result); err != nil {
return errors.Wrapf(err, "cannot decode result %s", string(bodyBytes))
}
if result.Status != "ok" {
return errors.New(fmt.Sprintf("error creating item: %s", result.Message))
}
return nil
}

func (fs *FairClient) WriteOriginalData(item *fair.ItemData, data []byte) error {
tr, err := JWTInterceptor.NewJWTTransport(
fs.service,
Expand Down
14 changes: 14 additions & 0 deletions pkg/service/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,20 @@ func (s *Server) ListenAndServe(cert, key string) (err error) {
s.log,
))).
Methods("POST")
router.Handle(
"/{partition}/source",
handlers.CompressHandler(
JWTInterceptor.JWTInterceptor(
s.service,
"setSource",
JWTInterceptor.Secure,
func() http.Handler { return http.HandlerFunc(s.setSourceHandler) }(),
s.jwtKey,
s.jwtAlg,
sha512.New(),
s.log,
))).
Methods("POST")
router.Handle(
"/{partition}/item/{uuid}/originaldata",
handlers.CompressHandler(
Expand Down
32 changes: 32 additions & 0 deletions pkg/service/serverHandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,38 @@ func (s *Server) createHandler(w http.ResponseWriter, req *http.Request) {
return
}

func (s *Server) setSourceHandler(w http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
pName := vars["partition"]

var data = &fair.Source{}
bdata, err := ioutil.ReadAll(req.Body)
if err != nil {
s.log.Errorf("cannot read request body: %v", err)
sendCreateResult(s.log, w, "error", fmt.Sprintf("cannot read request body: %v", err), nil)
return
}

if err := json.Unmarshal(bdata, data); err != nil {
s.log.Errorf("cannot unmarshal request body [%s]: %v", string(bdata), err)
sendCreateResult(s.log, w, "error", fmt.Sprintf("cannot unmarshal request body [%s]: %v", string(bdata), err), nil)
return
}
if data.Partition != pName {
s.log.Errorf("source and partition do not match %s != %s", data.Partition, pName)
sendCreateResult(s.log, w, "error", fmt.Sprintf("source and partition do not match %s != %s", data.Partition, pName), nil)
return
}

if err := s.fair.SetSource(data); err != nil {
s.log.Errorf("cannot create source %v: %v", data, err)
sendCreateResult(s.log, w, "error", fmt.Sprintf("cannot create source %v: %v", data, err), nil)
return
}
sendCreateResult(s.log, w, "ok", fmt.Sprintf("cannot create source %v: %v", data, err), nil)

}

type DataTableResult struct {
Draw int64 `json:"draw"`
RecordsTotal int64 `json:"recordsTotal"`
Expand Down

0 comments on commit d00127d

Please sign in to comment.