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
| func DeleteBook(id primitive.ObjectID) error { | |
| _, err := BooksCollection.DeleteOne(Ctx, bson.D{{"_id", id}}) | |
| if err != nil { | |
| return err | |
| } | |
| return nil | |
| } |
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
| type AuthorBooks struct { | |
| FullName string `bson:"full_name"` | |
| Books []Book | |
| } | |
| func FindAuthorBooks(fullName string) ([]Book, error) { | |
| matchStage := bson.D{{"$match", bson.D{{"full_name", fullName}}}} | |
| lookupStage := bson.D{{"$lookup", | |
| bson.D{{"from", "books"}, |
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
| func UpdateBook(id primitive.ObjectID, pageCount int) error { | |
| filter := bson.D{{"_id", id}} | |
| update := bson.D{{"$set", bson.D{{"page_count", pageCount}}}} | |
| _, err := BooksCollection.UpdateOne( | |
| Ctx, | |
| filter, | |
| update, | |
| ) | |
| return err | |
| } |
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
| func GetBooks() ([]Book, error) { | |
| var book Book | |
| var books []Book | |
| cursor, err := BooksCollection.Find(Ctx, bson.D{}) | |
| if err != nil { | |
| defer cursor.Close(Ctx) | |
| return books, err | |
| } |
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
| func GetBook(id string) (Book, error) { | |
| var b Book | |
| objectId, err := primitive.ObjectIDFromHex(id) | |
| if err != nil{ | |
| return b,err | |
| } | |
| err = BooksCollection. | |
| FindOne(Ctx, bson.D{{"_id", objectId}}). | |
| Decode(&b) |
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
| func CreateBook(b Book) (string, error) { | |
| result, err := BooksCollection.InsertOne(Ctx, b) | |
| if err != nil { | |
| return "0", err | |
| } | |
| return fmt.Sprintf("%v", result.InsertedID), err | |
| } |
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
| type Book struct { | |
| Name string `bson:"name"` | |
| Author string `bson:"author"` | |
| PageCount int `bson:"page_count"` | |
| } | |
| type Author struct { | |
| FullName string `bson:"full_name"` | |
| } |
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
| import ( | |
| "context" | |
| "go.mongodb.org/mongo-driver/mongo" | |
| "go.mongodb.org/mongo-driver/mongo/options" | |
| "log" | |
| ) | |
| var ( | |
| BooksCollection *mongo.Collection | |
| AuthorsCollection *mongo.Collection |
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
| func (suite *TestSuiteEnv)Test_GetBooks_EmptyResult() { | |
| req, w := setGetBooksRouter(suite.db) | |
| a := suite.Assert() | |
| a.Equal(http.MethodGet, req.Method, "HTTP request method error") | |
| a.Equal(http.StatusOK, w.Code, "HTTP request status code error") | |
| body, err := ioutil.ReadAll(w.Body) | |
| if err != nil { | |
| a.Error(err) |
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
| type TestSuiteEnv struct { | |
| suite.Suite | |
| db *gorm.DB | |
| } | |
| // Testler başlamadan önce çalıştırılıyor | |
| func (suite *TestSuiteEnv) SetupSuite() { | |
| database.Setup() | |
| suite.db = database.GetDB() | |
| } |
NewerOlder