-
Notifications
You must be signed in to change notification settings - Fork 37
feat: Add new API notebook #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
InertGas01
wants to merge
1
commit into
andy89923:stu/113
Choose a base branch
from
InertGas01:stu/113
base: stu/113
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,110 @@ | ||
| package sbi | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "github.com/andy89923/nf-example/internal/logger" | ||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| func (s *Server) getNotebookRoute() []Route { | ||
| return []Route{ | ||
| { | ||
| Name: "Show Note", | ||
| Method: http.MethodGet, | ||
| Pattern: "/:Title", | ||
| APIFunc: s.HTTPShowNote, | ||
| // Use | ||
| // curl -X GET http://127.0.0.163:8000/notebook/User_Guide -w "\n" | ||
| // "Title: User_Guide" | ||
| // " /<Title>/ Show the content of the note with title <Title>." | ||
| }, | ||
| { | ||
| Name: "Create Note", | ||
| Method: http.MethodPut, | ||
| Pattern: "/:Title/:Content", | ||
| APIFunc: s.HTTPCreateNote, | ||
| // Use | ||
| // curl -X PUT http://127.0.0.163:8000/notebook/New_Title/New_content -w "\n" | ||
| // "Title: New_Title" | ||
| // "Content:" | ||
| // " New_content" | ||
| }, | ||
| { | ||
| Name: "Update Note", | ||
| Method: http.MethodPost, | ||
| Pattern: "/:Title/:Content", | ||
| APIFunc: s.HTTPUpdateNote, | ||
| // Use | ||
| // curl -X POST http://127.0.0.163:8000/notebook/New_Title/New_content -w "\n" | ||
| // "Title: New_Title" | ||
| // "Content:" | ||
| // " New_content" | ||
| }, | ||
| { | ||
| Name: "Note Append with whitespace at front.", | ||
| Method: http.MethodPost, | ||
| Pattern: "/:Title/append/:Content_append", | ||
| APIFunc: s.HTTPNoteWhitespaceAppend, | ||
| // Use | ||
| // curl -X POST http://127.0.0.163:8000/notebook/New_Title/append/new_content_to_be_append. -w "\n" | ||
| // "Title: New_Title" | ||
| // "Content:" | ||
| // " New_content new_content_to_be_append." | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (s *Server) HTTPShowNote(c *gin.Context) { | ||
| logger.SBILog.Infof("In HTTPShowNote") | ||
|
|
||
| targetName := c.Param("Title") | ||
| if targetName == "" { | ||
| c.String(http.StatusBadRequest, "No name provided") | ||
| return | ||
| } | ||
|
|
||
| s.Processor().FindNote(c, targetName) | ||
| } | ||
|
|
||
| func (s *Server) HTTPUpdateNote(c *gin.Context) { | ||
| logger.SBILog.Infof("In HTTPUpdateNote") | ||
|
|
||
| targetName := c.Param("Title") | ||
| if targetName == "" { | ||
| c.String(http.StatusBadRequest, "No name provided") | ||
| return | ||
| } | ||
|
|
||
| newContent := c.Param("Content") | ||
|
|
||
| s.Processor().UpdateNote(c, targetName, newContent) | ||
| } | ||
|
|
||
| func (s *Server) HTTPCreateNote(c *gin.Context) { | ||
| logger.SBILog.Infof("In HTTPCreateNote") | ||
|
|
||
| targetName := c.Param("Title") | ||
| if targetName == "" { | ||
| c.String(http.StatusBadRequest, "No name provided") | ||
| return | ||
| } | ||
|
|
||
| newContent := c.Param("Content") | ||
|
|
||
| s.Processor().CreateNote(c, targetName, newContent) | ||
| } | ||
|
|
||
| func (s *Server) HTTPNoteWhitespaceAppend(c *gin.Context) { | ||
| logger.SBILog.Infof("In HTTPNoteAppend") | ||
|
|
||
| targetName := c.Param("Title") | ||
| if targetName == "" { | ||
| c.String(http.StatusBadRequest, "No name provided") | ||
| return | ||
| } | ||
|
|
||
| newContent := c.Param("Content_append") | ||
|
|
||
| s.Processor().NoteWhitespaceAppend(c, targetName, newContent) | ||
| } |
This file contains hidden or 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,102 @@ | ||
| package sbi_test | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/andy89923/nf-example/internal/sbi" | ||
| "github.com/andy89923/nf-example/pkg/factory" | ||
| "github.com/gin-gonic/gin" | ||
| "go.uber.org/mock/gomock" | ||
| ) | ||
|
|
||
| func Test_getNotebookRoutes(t *testing.T) { | ||
| gin.SetMode(gin.TestMode) | ||
|
|
||
| mockCtrl := gomock.NewController(t) | ||
| nfApp := sbi.NewMocknfApp(mockCtrl) | ||
| nfApp.EXPECT().Config().Return(&factory.Config{ | ||
| Configuration: &factory.Configuration{ | ||
| Sbi: &factory.Sbi{ | ||
| Port: 8000, | ||
| }, | ||
| }, | ||
| }).AnyTimes() | ||
| server := sbi.NewServer(nfApp, "") | ||
|
|
||
| t.Run("No name provided", func(t *testing.T) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "No name provided" ?
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The following test with same name. |
||
| const EXPECTED_STATUS = http.StatusBadRequest | ||
| const EXPECTED_BODY = "No name provided" | ||
|
|
||
| httpRecorder := httptest.NewRecorder() | ||
| ginCtx, _ := gin.CreateTestContext(httpRecorder) | ||
|
|
||
| var err error | ||
| ginCtx.Request, err = http.NewRequest("GET", "/notebook/", nil) | ||
| if err != nil { | ||
| t.Errorf("Failed to create request: %s", err) | ||
| return | ||
| } | ||
|
|
||
| server.HTTPShowNote(ginCtx) | ||
|
|
||
| if httpRecorder.Code != EXPECTED_STATUS { | ||
| t.Errorf("Expected status code %d, got %d", EXPECTED_STATUS, httpRecorder.Code) | ||
| } | ||
|
|
||
| if httpRecorder.Body.String() != EXPECTED_BODY { | ||
| t.Errorf("Expected body %s, got %s", EXPECTED_BODY, httpRecorder.Body.String()) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("No name provided", func(t *testing.T) { | ||
| const EXPECTED_STATUS = http.StatusBadRequest | ||
| const EXPECTED_BODY = "No name provided" | ||
|
|
||
| httpRecorder := httptest.NewRecorder() | ||
| ginCtx, _ := gin.CreateTestContext(httpRecorder) | ||
|
|
||
| var err error | ||
| ginCtx.Request, err = http.NewRequest("POST", "/notebook/", nil) | ||
| if err != nil { | ||
| t.Errorf("Failed to create request: %s", err) | ||
| return | ||
| } | ||
|
|
||
| server.HTTPUpdateNote(ginCtx) | ||
|
|
||
| if httpRecorder.Code != EXPECTED_STATUS { | ||
| t.Errorf("Expected status code %d, got %d", EXPECTED_STATUS, httpRecorder.Code) | ||
| } | ||
|
|
||
| if httpRecorder.Body.String() != EXPECTED_BODY { | ||
| t.Errorf("Expected body %s, got %s", EXPECTED_BODY, httpRecorder.Body.String()) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("No name provided", func(t *testing.T) { | ||
| const EXPECTED_STATUS = http.StatusBadRequest | ||
| const EXPECTED_BODY = "No name provided" | ||
|
|
||
| httpRecorder := httptest.NewRecorder() | ||
| ginCtx, _ := gin.CreateTestContext(httpRecorder) | ||
|
|
||
| var err error | ||
| ginCtx.Request, err = http.NewRequest("POST", "/notebook//append/", nil) | ||
| if err != nil { | ||
| t.Errorf("Failed to create request: %s", err) | ||
| return | ||
| } | ||
|
|
||
| server.HTTPUpdateNote(ginCtx) | ||
|
|
||
| if httpRecorder.Code != EXPECTED_STATUS { | ||
| t.Errorf("Expected status code %d, got %d", EXPECTED_STATUS, httpRecorder.Code) | ||
| } | ||
|
|
||
| if httpRecorder.Body.String() != EXPECTED_BODY { | ||
| t.Errorf("Expected body %s, got %s", EXPECTED_BODY, httpRecorder.Body.String()) | ||
| } | ||
| }) | ||
| } | ||
This file contains hidden or 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,58 @@ | ||
| package processor | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
| "strings" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| func (p *Processor) FindNote(c *gin.Context, targetName string) { | ||
| if content, ok := p.Context().NoteData[targetName]; ok { | ||
| c.String(http.StatusOK, fmt.Sprintf("Title: %s\nContent:\n\t%s\n", targetName, content)) | ||
| return | ||
| } | ||
| c.String(http.StatusNotFound, fmt.Sprintf("[%s] not found in Notebook\n", targetName)) | ||
| } | ||
|
|
||
| func (p *Processor) UpdateNote(c *gin.Context, targetName string, newContent string) { | ||
| p.Context().NoteData[targetName] = newContent | ||
|
|
||
| if content, ok := p.Context().NoteData[targetName]; ok { | ||
| c.String(http.StatusOK, fmt.Sprintf("Title: %s\nContent:\n\t%s\n", targetName, content)) | ||
| return | ||
| } | ||
| c.String(http.StatusNotFound, fmt.Sprintf("[%s] not found in Notebook\n", targetName)) | ||
| } | ||
|
|
||
| func (p *Processor) CreateNote(c *gin.Context, targetName string, newContent string) { | ||
| if _, ok := p.Context().NoteData[targetName]; !ok { | ||
| p.Context().NoteData[targetName] = newContent | ||
|
|
||
| content := p.Context().NoteData[targetName] | ||
| c.String(http.StatusOK, fmt.Sprintf("Title: %s\nContent:\n\t%s\n", targetName, content)) | ||
| return | ||
| } else if ok { | ||
| c.String(http.StatusForbidden, fmt.Sprintf( | ||
| "[%s] already exist. Please use POST to modify the content.\n", | ||
| targetName), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| func (p *Processor) NoteWhitespaceAppend(c *gin.Context, targetName string, newContent string) { | ||
| if _, ok := p.Context().NoteData[targetName]; ok { | ||
| var sb strings.Builder | ||
| sb.WriteString(p.Context().NoteData[targetName]) | ||
| sb.WriteString(" ") | ||
| sb.WriteString(newContent) | ||
|
|
||
| p.Context().NoteData[targetName] = sb.String() | ||
|
|
||
| content := p.Context().NoteData[targetName] | ||
| c.String(http.StatusOK, fmt.Sprintf("Title: %s\nContent:\n\t%s\n", targetName, content)) | ||
| return | ||
| } | ||
| c.String(http.StatusNotFound, fmt.Sprintf("[%s] not found in Notebook\n", targetName)) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please write the test for your service.