Skip to content

Commit

Permalink
[MI-3218]: Fixed review comments 'Add CRUD operations' (#3)
Browse files Browse the repository at this point in the history
* [MI-3218]:Fixed review comments 'Add CRUD operations'

* [MI-3218]:Fixed review comments
  • Loading branch information
Kshitij-Katiyar authored Jun 29, 2023
1 parent c2011b4 commit 0a0cf02
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
15 changes: 7 additions & 8 deletions server/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (h *Handler) setLink(w http.ResponseWriter, r *http.Request) {
func (h *Handler) deleteLink(w http.ResponseWriter, r *http.Request) {
autolinkName := r.URL.Query().Get(autolinkclient.AutolinkNameQueryParam)
if autolinkName == "" {
h.handleError(w, errors.New("autolink name or pattern should not be empty"))
h.handleError(w, errors.New("autolink name should not be empty"))
return
}

Expand All @@ -140,6 +140,7 @@ func (h *Handler) deleteLink(w http.ResponseWriter, r *http.Request) {
if links[i].Name == autolinkName {
links = append(links[:i], links[i+1:]...)
found = true
break
}
}

Expand All @@ -164,21 +165,19 @@ func (h *Handler) getLinks(w http.ResponseWriter, r *http.Request) {
return
}

found := false
var autolinks []autolink.Autolink
var autolink *autolink.Autolink
for _, link := range links {
if link.Name == autolinkName {
autolinks = append(autolinks, link)
found = true
autolink = &link
break
}
}

if !found {
if autolink == nil {
h.handleError(w, errors.Errorf("no autolink found with name %s", autolinkName))
return
}

h.handleSendingJSONContent(w, autolinks)
h.handleSendingJSONContent(w, autolink)
return
}

Expand Down
17 changes: 10 additions & 7 deletions server/autolinkclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (c *Client) Add(links ...autolink.Autolink) error {
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
respBody, _ := ioutil.ReadAll(resp.Body)
Expand All @@ -73,6 +74,7 @@ func (c *Client) Delete(links ...string) error {
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
respBody, _ := ioutil.ReadAll(resp.Body)
Expand All @@ -83,7 +85,7 @@ func (c *Client) Delete(links ...string) error {
return nil
}

func (c *Client) Get(autolinkName string) ([]*autolink.Autolink, error) {
func (c *Client) Get(autolinkName string) (*autolink.Autolink, error) {
queryParams := url.Values{
AutolinkNameQueryParam: {autolinkName},
}
Expand All @@ -92,17 +94,18 @@ func (c *Client) Get(autolinkName string) ([]*autolink.Autolink, error) {
if err != nil {
return nil, err
}
defer resp.Body.Close()

respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

var respBody []byte
if resp.StatusCode != http.StatusOK {
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("unable to get the link %s. Error: %v, %v", autolinkName, resp.StatusCode, string(respBody))
}

var response []*autolink.Autolink
var response *autolink.Autolink
if err = json.Unmarshal(respBody, &response); err != nil {
return nil, err
}
Expand Down

0 comments on commit 0a0cf02

Please sign in to comment.