-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from go-park-mail-ru/dev4
Dev4
- Loading branch information
Showing
47 changed files
with
2,895 additions
and
309 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
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,119 @@ | ||
package v1 | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/entity/comment" | ||
"github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/entity/user" | ||
"github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/middleware/auth" | ||
"github.com/mailru/easyjson" | ||
) | ||
|
||
func (h *HandlerHTTP) WriteComment(w http.ResponseWriter, r *http.Request) { | ||
logger := h.getRequestLogger(r) | ||
userID := r.Context().Value(auth.KeyCurrentUserID).(int) | ||
|
||
pinID, err := fetchURLParamInt(r, "pinID") | ||
if err != nil { | ||
err = responseError(w, "parse_url", "the request url could not be get pin id") | ||
if err != nil { | ||
logger.Error(err.Error()) | ||
return | ||
} | ||
} | ||
|
||
comment := &comment.Comment{} | ||
|
||
err = easyjson.UnmarshalFromReader(r.Body, comment) | ||
defer r.Body.Close() | ||
if err != nil { | ||
logger.Warn(err.Error()) | ||
|
||
err = responseError(w, "parse_body", "the request body could not be parsed to send a comment") | ||
if err != nil { | ||
logger.Error(err.Error()) | ||
return | ||
} | ||
} | ||
|
||
comment.PinID = pinID | ||
_, err = h.commentCase.PutCommentOnPin(r.Context(), userID, comment) | ||
if err != nil { | ||
logger.Error(err.Error()) | ||
err = responseError(w, "create_comment", "couldn't leave a comment under the selected pin") | ||
} else { | ||
err = responseOk(http.StatusCreated, w, "the comment has been added successfully", nil) | ||
} | ||
if err != nil { | ||
logger.Error(err.Error()) | ||
} | ||
} | ||
|
||
func (h *HandlerHTTP) DeleteComment(w http.ResponseWriter, r *http.Request) { | ||
logger := h.getRequestLogger(r) | ||
userID := r.Context().Value(auth.KeyCurrentUserID).(int) | ||
|
||
commentID, err := fetchURLParamInt(r, "commentID") | ||
if err != nil { | ||
err = responseError(w, "parse_url", "the request url could not be get pin id") | ||
if err != nil { | ||
logger.Error(err.Error()) | ||
return | ||
} | ||
} | ||
|
||
err = h.commentCase.DeleteComment(r.Context(), userID, commentID) | ||
if err != nil { | ||
logger.Error(err.Error()) | ||
err = responseError(w, "delete_comment", "couldn't delete pin comment") | ||
} else { | ||
err = responseOk(http.StatusOK, w, "the comment was successfully deleted", nil) | ||
} | ||
if err != nil { | ||
logger.Error(err.Error()) | ||
} | ||
} | ||
|
||
func (h *HandlerHTTP) ViewFeedComment(w http.ResponseWriter, r *http.Request) { | ||
logger := h.getRequestLogger(r) | ||
userID, ok := r.Context().Value(auth.KeyCurrentUserID).(int) | ||
if !ok { | ||
userID = user.UserUnknown | ||
} | ||
|
||
pinID, err := fetchURLParamInt(r, "pinID") | ||
if err != nil { | ||
err = responseError(w, "parse_url", "the request url could not be get pin id") | ||
if err != nil { | ||
logger.Error(err.Error()) | ||
return | ||
} | ||
} | ||
|
||
count, lastID, err := FetchValidParamForLoadFeed(r.URL) | ||
if err != nil { | ||
err = responseError(w, "query_param", "the parameters for displaying the pin feed could not be extracted from the request") | ||
if err != nil { | ||
logger.Error(err.Error()) | ||
return | ||
} | ||
} | ||
|
||
feed, newLastID, err := h.commentCase.GetFeedCommentOnPin(r.Context(), userID, pinID, count, lastID) | ||
if err != nil && len(feed) == 0 { | ||
err = responseError(w, "feed_view", "error displaying pin comments") | ||
if err != nil { | ||
logger.Error(err.Error()) | ||
} | ||
return | ||
} | ||
|
||
if err != nil { | ||
logger.Error(err.Error()) | ||
} | ||
|
||
err = responseOk(http.StatusOK, w, "feed comment to pin", map[string]any{"comments": feed, "lastID": newLastID}) | ||
if err != nil { | ||
logger.Error(err.Error()) | ||
} | ||
} |
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
Oops, something went wrong.