diff --git a/api/blog.go b/api/post.go similarity index 95% rename from api/blog.go rename to api/post.go index b11d6c1..46e1c51 100644 --- a/api/blog.go +++ b/api/post.go @@ -50,12 +50,12 @@ func (server *Server) GetBlogs(ctx *gin.Context) { } - arg := db.GetBlogsParams{ + arg := db.GetPostsParams{ Limit: int32(size), Offset: int32((page - 1) * size), } - blogs, err := server.store.GetBlogs(ctx, arg) + blogs, err := server.store.GetPosts(ctx, arg) if err != nil { BuildResponse(ctx, BaseResponse{ Code: http.StatusInternalServerError, diff --git a/db/migration/000001_initial.down.sql b/db/migration/000001_initial.down.sql index afcecc2..cae8256 100644 --- a/db/migration/000001_initial.down.sql +++ b/db/migration/000001_initial.down.sql @@ -1,33 +1,33 @@ -- Drop foreign key constraints first ALTER TABLE "sessions" DROP CONSTRAINT "sessions_user_id_fkey"; -ALTER TABLE "blog_likes" DROP CONSTRAINT "blog_likes_user_id_fkey"; -ALTER TABLE "blog_likes" DROP CONSTRAINT "blog_likes_blog_id_fkey"; +ALTER TABLE "post_likes" DROP CONSTRAINT "post_likes_user_id_fkey"; +ALTER TABLE "post_likes" DROP CONSTRAINT "post_likes_post_id_fkey"; ALTER TABLE "comments" DROP CONSTRAINT "comments_user_id_fkey"; -ALTER TABLE "comments" DROP CONSTRAINT "comments_blog_id_fkey"; +ALTER TABLE "comments" DROP CONSTRAINT "comments_post_id_fkey"; ALTER TABLE "user_followers" DROP CONSTRAINT "user_followers_follower_id_fkey"; ALTER TABLE "user_followers" DROP CONSTRAINT "user_followers_user_id_fkey"; -ALTER TABLE "user_posts" DROP CONSTRAINT "user_posts_blog_id_fkey"; +ALTER TABLE "user_posts" DROP CONSTRAINT "user_posts_post_id_fkey"; ALTER TABLE "user_posts" DROP CONSTRAINT "user_posts_user_id_fkey"; ALTER TABLE "profiles" DROP CONSTRAINT "profiles_user_id_fkey"; -ALTER TABLE "featured_stories" DROP CONSTRAINT "featured_stories_blog_id_fkey"; -ALTER TABLE "blog_categories" DROP CONSTRAINT "blog_categories_category_id_fkey"; -ALTER TABLE "blog_categories" DROP CONSTRAINT "blog_categories_blog_id_fkey"; -ALTER TABLE "blog_tags" DROP CONSTRAINT "blog_tags_tag_id_fkey"; -ALTER TABLE "blog_tags" DROP CONSTRAINT "blog_tags_blog_id_fkey"; -ALTER TABLE "blogs" DROP CONSTRAINT "blogs_user_id_fkey"; +ALTER TABLE "featured_stories" DROP CONSTRAINT "featured_stories_post_id_fkey"; +ALTER TABLE "post_categories" DROP CONSTRAINT "post_categories_category_id_fkey"; +ALTER TABLE "post_categories" DROP CONSTRAINT "post_categories_post_id_fkey"; +ALTER TABLE "post_tags" DROP CONSTRAINT "post_tags_tag_id_fkey"; +ALTER TABLE "post_tags" DROP CONSTRAINT "post_tags_post_id_fkey"; +ALTER TABLE "posts" DROP CONSTRAINT "posts_user_id_fkey"; -- Drop tables in reverse order of creation DROP TABLE IF EXISTS "sessions"; -DROP TABLE IF EXISTS "blog_likes"; +DROP TABLE IF EXISTS "post_likes"; DROP TABLE IF EXISTS "comments"; DROP TABLE IF EXISTS "user_followers"; DROP TABLE IF EXISTS "user_posts"; DROP TABLE IF EXISTS "profiles"; DROP TABLE IF EXISTS "featured_stories"; -DROP TABLE IF EXISTS "blog_categories"; -DROP TABLE IF EXISTS "blog_tags"; +DROP TABLE IF EXISTS "post_categories"; +DROP TABLE IF EXISTS "post_tags"; DROP TABLE IF EXISTS "tags"; -DROP TABLE IF EXISTS "blogs"; +DROP TABLE IF EXISTS "posts"; DROP TABLE IF EXISTS "categories"; DROP TABLE IF EXISTS "users"; DROP TABLE IF EXISTS "onboarding"; \ No newline at end of file diff --git a/db/migration/000001_initial.up.sql b/db/migration/000001_initial.up.sql index 0107aca..2bd8c77 100644 --- a/db/migration/000001_initial.up.sql +++ b/db/migration/000001_initial.up.sql @@ -23,7 +23,7 @@ CREATE TABLE "categories" ( "name" varchar(50) UNIQUE NOT NULL ); -CREATE TABLE "blogs" ( +CREATE TABLE "posts" ( "id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, "user_id" int, "title" varchar(255) NOT NULL, @@ -40,36 +40,36 @@ CREATE TABLE "tags" ( "name" varchar(50) UNIQUE NOT NULL ); -CREATE TABLE "blog_tags" ( - "blog_id" int NOT NULL, +CREATE TABLE "post_tags" ( + "post_id" int NOT NULL, "tag_id" int NOT NULL, - PRIMARY KEY ("blog_id", "tag_id") + PRIMARY KEY ("post_id", "tag_id") ); -CREATE TABLE "blog_categories" ( - "blog_id" int NOT NULL, +CREATE TABLE "post_categories" ( + "post_id" int NOT NULL, "category_id" int NOT NULL, - PRIMARY KEY ("blog_id", "category_id") + PRIMARY KEY ("post_id", "category_id") ); CREATE TABLE "featured_stories" ( "id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, - "blog_id" int NOT NULL, + "post_id" int NOT NULL, "featured_date" date DEFAULT (current_date) ); CREATE TABLE "profiles" ( "user_id" int PRIMARY KEY, "bio" text, - "blog_count" int DEFAULT 0, + "post_count" int DEFAULT 0, "like_count" int DEFAULT 0, "follower_count" int DEFAULT 0 ); CREATE TABLE "user_posts" ( "user_id" int NOT NULL, - "blog_id" int NOT NULL, - PRIMARY KEY ("user_id", "blog_id") + "post_id" int NOT NULL, + PRIMARY KEY ("user_id", "post_id") ); CREATE TABLE "user_followers" ( @@ -80,16 +80,16 @@ CREATE TABLE "user_followers" ( CREATE TABLE "comments" ( "id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, - "blog_id" int NOT NULL, + "post_id" int NOT NULL, "user_id" int NOT NULL, "content" text NOT NULL, "created_at" timestamptz DEFAULT (now()) ); -CREATE TABLE "blog_likes" ( - "blog_id" int NOT NULL, +CREATE TABLE "post_likes" ( + "post_id" int NOT NULL, "user_id" int NOT NULL, - PRIMARY KEY ("blog_id", "user_id") + PRIMARY KEY ("post_id", "user_id") ); CREATE TABLE "sessions" ( @@ -103,34 +103,34 @@ CREATE TABLE "sessions" ( "created_at" timestamptz NOT NULL DEFAULT (now()) ); -ALTER TABLE "blogs" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id"); +ALTER TABLE "posts" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id"); -ALTER TABLE "blog_tags" ADD FOREIGN KEY ("blog_id") REFERENCES "blogs" ("id"); +ALTER TABLE "post_tags" ADD FOREIGN KEY ("post_id") REFERENCES "posts" ("id"); -ALTER TABLE "blog_tags" ADD FOREIGN KEY ("tag_id") REFERENCES "tags" ("id"); +ALTER TABLE "post_tags" ADD FOREIGN KEY ("tag_id") REFERENCES "tags" ("id"); -ALTER TABLE "blog_categories" ADD FOREIGN KEY ("blog_id") REFERENCES "blogs" ("id"); +ALTER TABLE "post_categories" ADD FOREIGN KEY ("post_id") REFERENCES "posts" ("id"); -ALTER TABLE "blog_categories" ADD FOREIGN KEY ("category_id") REFERENCES "categories" ("id"); +ALTER TABLE "post_categories" ADD FOREIGN KEY ("category_id") REFERENCES "categories" ("id"); -ALTER TABLE "featured_stories" ADD FOREIGN KEY ("blog_id") REFERENCES "blogs" ("id"); +ALTER TABLE "featured_stories" ADD FOREIGN KEY ("post_id") REFERENCES "posts" ("id"); ALTER TABLE "profiles" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id"); ALTER TABLE "user_posts" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id"); -ALTER TABLE "user_posts" ADD FOREIGN KEY ("blog_id") REFERENCES "blogs" ("id"); +ALTER TABLE "user_posts" ADD FOREIGN KEY ("post_id") REFERENCES "posts" ("id"); ALTER TABLE "user_followers" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id"); ALTER TABLE "user_followers" ADD FOREIGN KEY ("follower_id") REFERENCES "users" ("id"); -ALTER TABLE "comments" ADD FOREIGN KEY ("blog_id") REFERENCES "blogs" ("id"); +ALTER TABLE "comments" ADD FOREIGN KEY ("post_id") REFERENCES "posts" ("id"); ALTER TABLE "comments" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id"); -ALTER TABLE "blog_likes" ADD FOREIGN KEY ("blog_id") REFERENCES "blogs" ("id"); +ALTER TABLE "post_likes" ADD FOREIGN KEY ("post_id") REFERENCES "posts" ("id"); -ALTER TABLE "blog_likes" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id"); +ALTER TABLE "post_likes" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id"); ALTER TABLE "sessions" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id"); diff --git a/db/mock/store.go b/db/mock/store.go index 018fae9..298560a 100644 --- a/db/mock/store.go +++ b/db/mock/store.go @@ -36,48 +36,6 @@ func (m *MockStore) EXPECT() *MockStoreMockRecorder { return m.recorder } -// DeleteBlog mocks base method. -func (m *MockStore) DeleteBlog(arg0 context.Context, arg1 int32) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "DeleteBlog", arg0, arg1) - ret0, _ := ret[0].(error) - return ret0 -} - -// DeleteBlog indicates an expected call of DeleteBlog. -func (mr *MockStoreMockRecorder) DeleteBlog(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteBlog", reflect.TypeOf((*MockStore)(nil).DeleteBlog), arg0, arg1) -} - -// DeleteBlogCategory mocks base method. -func (m *MockStore) DeleteBlogCategory(arg0 context.Context, arg1 db.DeleteBlogCategoryParams) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "DeleteBlogCategory", arg0, arg1) - ret0, _ := ret[0].(error) - return ret0 -} - -// DeleteBlogCategory indicates an expected call of DeleteBlogCategory. -func (mr *MockStoreMockRecorder) DeleteBlogCategory(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteBlogCategory", reflect.TypeOf((*MockStore)(nil).DeleteBlogCategory), arg0, arg1) -} - -// DeleteBlogTag mocks base method. -func (m *MockStore) DeleteBlogTag(arg0 context.Context, arg1 db.DeleteBlogTagParams) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "DeleteBlogTag", arg0, arg1) - ret0, _ := ret[0].(error) - return ret0 -} - -// DeleteBlogTag indicates an expected call of DeleteBlogTag. -func (mr *MockStoreMockRecorder) DeleteBlogTag(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteBlogTag", reflect.TypeOf((*MockStore)(nil).DeleteBlogTag), arg0, arg1) -} - // DeleteCategory mocks base method. func (m *MockStore) DeleteCategory(arg0 context.Context, arg1 int32) error { m.ctrl.T.Helper() @@ -134,6 +92,48 @@ func (mr *MockStoreMockRecorder) DeleteOnboarding(arg0, arg1 interface{}) *gomoc return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteOnboarding", reflect.TypeOf((*MockStore)(nil).DeleteOnboarding), arg0, arg1) } +// DeletePost mocks base method. +func (m *MockStore) DeletePost(arg0 context.Context, arg1 int32) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeletePost", arg0, arg1) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeletePost indicates an expected call of DeletePost. +func (mr *MockStoreMockRecorder) DeletePost(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeletePost", reflect.TypeOf((*MockStore)(nil).DeletePost), arg0, arg1) +} + +// DeletePostCategory mocks base method. +func (m *MockStore) DeletePostCategory(arg0 context.Context, arg1 db.DeletePostCategoryParams) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeletePostCategory", arg0, arg1) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeletePostCategory indicates an expected call of DeletePostCategory. +func (mr *MockStoreMockRecorder) DeletePostCategory(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeletePostCategory", reflect.TypeOf((*MockStore)(nil).DeletePostCategory), arg0, arg1) +} + +// DeletePostTag mocks base method. +func (m *MockStore) DeletePostTag(arg0 context.Context, arg1 db.DeletePostTagParams) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeletePostTag", arg0, arg1) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeletePostTag indicates an expected call of DeletePostTag. +func (mr *MockStoreMockRecorder) DeletePostTag(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeletePostTag", reflect.TypeOf((*MockStore)(nil).DeletePostTag), arg0, arg1) +} + // DeleteProfile mocks base method. func (m *MockStore) DeleteProfile(arg0 context.Context, arg1 int32) error { m.ctrl.T.Helper() @@ -204,49 +204,19 @@ func (mr *MockStoreMockRecorder) DeleteUserPost(arg0, arg1 interface{}) *gomock. return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteUserPost", reflect.TypeOf((*MockStore)(nil).DeleteUserPost), arg0, arg1) } -// GetBlog mocks base method. -func (m *MockStore) GetBlog(arg0 context.Context, arg1 int32) (db.Blog, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetBlog", arg0, arg1) - ret0, _ := ret[0].(db.Blog) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// GetBlog indicates an expected call of GetBlog. -func (mr *MockStoreMockRecorder) GetBlog(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetBlog", reflect.TypeOf((*MockStore)(nil).GetBlog), arg0, arg1) -} - -// GetBlogs mocks base method. -func (m *MockStore) GetBlogs(arg0 context.Context, arg1 db.GetBlogsParams) ([]db.Blog, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetBlogs", arg0, arg1) - ret0, _ := ret[0].([]db.Blog) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// GetBlogs indicates an expected call of GetBlogs. -func (mr *MockStoreMockRecorder) GetBlogs(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetBlogs", reflect.TypeOf((*MockStore)(nil).GetBlogs), arg0, arg1) -} - -// GetCategoriesForBlog mocks base method. -func (m *MockStore) GetCategoriesForBlog(arg0 context.Context, arg1 int32) ([]db.Category, error) { +// GetCategoriesForPost mocks base method. +func (m *MockStore) GetCategoriesForPost(arg0 context.Context, arg1 int32) ([]db.Category, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetCategoriesForBlog", arg0, arg1) + ret := m.ctrl.Call(m, "GetCategoriesForPost", arg0, arg1) ret0, _ := ret[0].([]db.Category) ret1, _ := ret[1].(error) return ret0, ret1 } -// GetCategoriesForBlog indicates an expected call of GetCategoriesForBlog. -func (mr *MockStoreMockRecorder) GetCategoriesForBlog(arg0, arg1 interface{}) *gomock.Call { +// GetCategoriesForPost indicates an expected call of GetCategoriesForPost. +func (mr *MockStoreMockRecorder) GetCategoriesForPost(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetCategoriesForBlog", reflect.TypeOf((*MockStore)(nil).GetCategoriesForBlog), arg0, arg1) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetCategoriesForPost", reflect.TypeOf((*MockStore)(nil).GetCategoriesForPost), arg0, arg1) } // GetCategory mocks base method. @@ -264,19 +234,19 @@ func (mr *MockStoreMockRecorder) GetCategory(arg0, arg1 interface{}) *gomock.Cal return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetCategory", reflect.TypeOf((*MockStore)(nil).GetCategory), arg0, arg1) } -// GetCommentsForBlog mocks base method. -func (m *MockStore) GetCommentsForBlog(arg0 context.Context, arg1 int32) ([]db.Comment, error) { +// GetCommentsForPost mocks base method. +func (m *MockStore) GetCommentsForPost(arg0 context.Context, arg1 int32) ([]db.Comment, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetCommentsForBlog", arg0, arg1) + ret := m.ctrl.Call(m, "GetCommentsForPost", arg0, arg1) ret0, _ := ret[0].([]db.Comment) ret1, _ := ret[1].(error) return ret0, ret1 } -// GetCommentsForBlog indicates an expected call of GetCommentsForBlog. -func (mr *MockStoreMockRecorder) GetCommentsForBlog(arg0, arg1 interface{}) *gomock.Call { +// GetCommentsForPost indicates an expected call of GetCommentsForPost. +func (mr *MockStoreMockRecorder) GetCommentsForPost(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetCommentsForBlog", reflect.TypeOf((*MockStore)(nil).GetCommentsForBlog), arg0, arg1) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetCommentsForPost", reflect.TypeOf((*MockStore)(nil).GetCommentsForPost), arg0, arg1) } // GetFeaturedStory mocks base method. @@ -339,6 +309,36 @@ func (mr *MockStoreMockRecorder) GetOnboarding(arg0, arg1 interface{}) *gomock.C return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetOnboarding", reflect.TypeOf((*MockStore)(nil).GetOnboarding), arg0, arg1) } +// GetPost mocks base method. +func (m *MockStore) GetPost(arg0 context.Context, arg1 int32) (db.Post, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetPost", arg0, arg1) + ret0, _ := ret[0].(db.Post) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetPost indicates an expected call of GetPost. +func (mr *MockStoreMockRecorder) GetPost(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetPost", reflect.TypeOf((*MockStore)(nil).GetPost), arg0, arg1) +} + +// GetPosts mocks base method. +func (m *MockStore) GetPosts(arg0 context.Context, arg1 db.GetPostsParams) ([]db.Post, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetPosts", arg0, arg1) + ret0, _ := ret[0].([]db.Post) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetPosts indicates an expected call of GetPosts. +func (mr *MockStoreMockRecorder) GetPosts(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetPosts", reflect.TypeOf((*MockStore)(nil).GetPosts), arg0, arg1) +} + // GetProfile mocks base method. func (m *MockStore) GetProfile(arg0 context.Context, arg1 int32) (db.Profile, error) { m.ctrl.T.Helper() @@ -384,19 +384,19 @@ func (mr *MockStoreMockRecorder) GetTag(arg0, arg1 interface{}) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetTag", reflect.TypeOf((*MockStore)(nil).GetTag), arg0, arg1) } -// GetTagsForBlog mocks base method. -func (m *MockStore) GetTagsForBlog(arg0 context.Context, arg1 int32) ([]db.Tag, error) { +// GetTagsForPost mocks base method. +func (m *MockStore) GetTagsForPost(arg0 context.Context, arg1 int32) ([]db.Tag, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetTagsForBlog", arg0, arg1) + ret := m.ctrl.Call(m, "GetTagsForPost", arg0, arg1) ret0, _ := ret[0].([]db.Tag) ret1, _ := ret[1].(error) return ret0, ret1 } -// GetTagsForBlog indicates an expected call of GetTagsForBlog. -func (mr *MockStoreMockRecorder) GetTagsForBlog(arg0, arg1 interface{}) *gomock.Call { +// GetTagsForPost indicates an expected call of GetTagsForPost. +func (mr *MockStoreMockRecorder) GetTagsForPost(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetTagsForBlog", reflect.TypeOf((*MockStore)(nil).GetTagsForBlog), arg0, arg1) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetTagsForPost", reflect.TypeOf((*MockStore)(nil).GetTagsForPost), arg0, arg1) } // GetUser mocks base method. @@ -414,79 +414,34 @@ func (mr *MockStoreMockRecorder) GetUser(arg0, arg1 interface{}) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUser", reflect.TypeOf((*MockStore)(nil).GetUser), arg0, arg1) } -// GetUserBlog mocks base method. -func (m *MockStore) GetUserBlog(arg0 context.Context, arg1 db.GetUserBlogParams) (db.GetUserBlogRow, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetUserBlog", arg0, arg1) - ret0, _ := ret[0].(db.GetUserBlogRow) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// GetUserBlog indicates an expected call of GetUserBlog. -func (mr *MockStoreMockRecorder) GetUserBlog(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUserBlog", reflect.TypeOf((*MockStore)(nil).GetUserBlog), arg0, arg1) -} - -// GetUserBlogs mocks base method. -func (m *MockStore) GetUserBlogs(arg0 context.Context, arg1 int32) ([]db.GetUserBlogsRow, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetUserBlogs", arg0, arg1) - ret0, _ := ret[0].([]db.GetUserBlogsRow) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// GetUserBlogs indicates an expected call of GetUserBlogs. -func (mr *MockStoreMockRecorder) GetUserBlogs(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUserBlogs", reflect.TypeOf((*MockStore)(nil).GetUserBlogs), arg0, arg1) -} - -// InsertBlog mocks base method. -func (m *MockStore) InsertBlog(arg0 context.Context, arg1 db.InsertBlogParams) (db.Blog, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "InsertBlog", arg0, arg1) - ret0, _ := ret[0].(db.Blog) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// InsertBlog indicates an expected call of InsertBlog. -func (mr *MockStoreMockRecorder) InsertBlog(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InsertBlog", reflect.TypeOf((*MockStore)(nil).InsertBlog), arg0, arg1) -} - -// InsertBlogCategory mocks base method. -func (m *MockStore) InsertBlogCategory(arg0 context.Context, arg1 db.InsertBlogCategoryParams) (db.BlogCategory, error) { +// GetUserPost mocks base method. +func (m *MockStore) GetUserPost(arg0 context.Context, arg1 db.GetUserPostParams) (db.GetUserPostRow, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "InsertBlogCategory", arg0, arg1) - ret0, _ := ret[0].(db.BlogCategory) + ret := m.ctrl.Call(m, "GetUserPost", arg0, arg1) + ret0, _ := ret[0].(db.GetUserPostRow) ret1, _ := ret[1].(error) return ret0, ret1 } -// InsertBlogCategory indicates an expected call of InsertBlogCategory. -func (mr *MockStoreMockRecorder) InsertBlogCategory(arg0, arg1 interface{}) *gomock.Call { +// GetUserPost indicates an expected call of GetUserPost. +func (mr *MockStoreMockRecorder) GetUserPost(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InsertBlogCategory", reflect.TypeOf((*MockStore)(nil).InsertBlogCategory), arg0, arg1) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUserPost", reflect.TypeOf((*MockStore)(nil).GetUserPost), arg0, arg1) } -// InsertBlogTag mocks base method. -func (m *MockStore) InsertBlogTag(arg0 context.Context, arg1 db.InsertBlogTagParams) (db.BlogTag, error) { +// GetUserPosts mocks base method. +func (m *MockStore) GetUserPosts(arg0 context.Context, arg1 int32) ([]db.GetUserPostsRow, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "InsertBlogTag", arg0, arg1) - ret0, _ := ret[0].(db.BlogTag) + ret := m.ctrl.Call(m, "GetUserPosts", arg0, arg1) + ret0, _ := ret[0].([]db.GetUserPostsRow) ret1, _ := ret[1].(error) return ret0, ret1 } -// InsertBlogTag indicates an expected call of InsertBlogTag. -func (mr *MockStoreMockRecorder) InsertBlogTag(arg0, arg1 interface{}) *gomock.Call { +// GetUserPosts indicates an expected call of GetUserPosts. +func (mr *MockStoreMockRecorder) GetUserPosts(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InsertBlogTag", reflect.TypeOf((*MockStore)(nil).InsertBlogTag), arg0, arg1) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUserPosts", reflect.TypeOf((*MockStore)(nil).GetUserPosts), arg0, arg1) } // InsertCategory mocks base method. @@ -549,6 +504,51 @@ func (mr *MockStoreMockRecorder) InsertOnboarding(arg0, arg1 interface{}) *gomoc return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InsertOnboarding", reflect.TypeOf((*MockStore)(nil).InsertOnboarding), arg0, arg1) } +// InsertPost mocks base method. +func (m *MockStore) InsertPost(arg0 context.Context, arg1 db.InsertPostParams) (db.Post, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "InsertPost", arg0, arg1) + ret0, _ := ret[0].(db.Post) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// InsertPost indicates an expected call of InsertPost. +func (mr *MockStoreMockRecorder) InsertPost(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InsertPost", reflect.TypeOf((*MockStore)(nil).InsertPost), arg0, arg1) +} + +// InsertPostCategory mocks base method. +func (m *MockStore) InsertPostCategory(arg0 context.Context, arg1 db.InsertPostCategoryParams) (db.PostCategory, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "InsertPostCategory", arg0, arg1) + ret0, _ := ret[0].(db.PostCategory) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// InsertPostCategory indicates an expected call of InsertPostCategory. +func (mr *MockStoreMockRecorder) InsertPostCategory(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InsertPostCategory", reflect.TypeOf((*MockStore)(nil).InsertPostCategory), arg0, arg1) +} + +// InsertPostTag mocks base method. +func (m *MockStore) InsertPostTag(arg0 context.Context, arg1 db.InsertPostTagParams) (db.PostTag, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "InsertPostTag", arg0, arg1) + ret0, _ := ret[0].(db.PostTag) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// InsertPostTag indicates an expected call of InsertPostTag. +func (mr *MockStoreMockRecorder) InsertPostTag(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InsertPostTag", reflect.TypeOf((*MockStore)(nil).InsertPostTag), arg0, arg1) +} + // InsertProfile mocks base method. func (m *MockStore) InsertProfile(arg0 context.Context, arg1 db.InsertProfileParams) (db.Profile, error) { m.ctrl.T.Helper() @@ -654,21 +654,6 @@ func (mr *MockStoreMockRecorder) ListOnboarding(arg0 interface{}) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListOnboarding", reflect.TypeOf((*MockStore)(nil).ListOnboarding), arg0) } -// UpdateBlog mocks base method. -func (m *MockStore) UpdateBlog(arg0 context.Context, arg1 db.UpdateBlogParams) (db.Blog, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "UpdateBlog", arg0, arg1) - ret0, _ := ret[0].(db.Blog) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// UpdateBlog indicates an expected call of UpdateBlog. -func (mr *MockStoreMockRecorder) UpdateBlog(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateBlog", reflect.TypeOf((*MockStore)(nil).UpdateBlog), arg0, arg1) -} - // UpdateCategory mocks base method. func (m *MockStore) UpdateCategory(arg0 context.Context, arg1 db.UpdateCategoryParams) (db.Category, error) { m.ctrl.T.Helper() @@ -714,6 +699,21 @@ func (mr *MockStoreMockRecorder) UpdateOnboarding(arg0, arg1 interface{}) *gomoc return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateOnboarding", reflect.TypeOf((*MockStore)(nil).UpdateOnboarding), arg0, arg1) } +// UpdatePost mocks base method. +func (m *MockStore) UpdatePost(arg0 context.Context, arg1 db.UpdatePostParams) (db.Post, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UpdatePost", arg0, arg1) + ret0, _ := ret[0].(db.Post) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// UpdatePost indicates an expected call of UpdatePost. +func (mr *MockStoreMockRecorder) UpdatePost(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdatePost", reflect.TypeOf((*MockStore)(nil).UpdatePost), arg0, arg1) +} + // UpdateProfile mocks base method. func (m *MockStore) UpdateProfile(arg0 context.Context, arg1 db.UpdateProfileParams) (db.Profile, error) { m.ctrl.T.Helper() diff --git a/db/query/blog_categories.sql b/db/query/blog_categories.sql deleted file mode 100644 index 6cc282d..0000000 --- a/db/query/blog_categories.sql +++ /dev/null @@ -1,14 +0,0 @@ --- name: InsertBlogCategory :one -INSERT INTO blog_categories (blog_id, category_id) -VALUES ($1, $2) -RETURNING *; - --- name: GetCategoriesForBlog :many -SELECT c.id, c.name -FROM categories c -JOIN blog_categories bc ON bc.category_id = c.id -WHERE bc.blog_id = $1; - --- name: DeleteBlogCategory :exec -DELETE FROM blog_categories -WHERE blog_id = $1 AND category_id = $2; \ No newline at end of file diff --git a/db/query/blog_tags.sql b/db/query/blog_tags.sql deleted file mode 100644 index 076200a..0000000 --- a/db/query/blog_tags.sql +++ /dev/null @@ -1,14 +0,0 @@ --- name: InsertBlogTag :one -INSERT INTO blog_tags (blog_id, tag_id) -VALUES ($1, $2) -RETURNING *; - --- name: GetTagsForBlog :many -SELECT t.id, t.name -FROM tags t -JOIN blog_tags bt ON bt.tag_id = t.id -WHERE bt.blog_id = $1; - --- name: DeleteBlogTag :exec -DELETE FROM blog_tags -WHERE blog_id = $1 AND tag_id = $2; \ No newline at end of file diff --git a/db/query/comments.sql b/db/query/comments.sql index 711c228..13e1d03 100644 --- a/db/query/comments.sql +++ b/db/query/comments.sql @@ -1,12 +1,12 @@ -- name: InsertComment :one -INSERT INTO comments (blog_id, user_id, content, created_at) +INSERT INTO comments (post_id, user_id, content, created_at) VALUES ($1, $2, $3, $4) RETURNING *; --- name: GetCommentsForBlog :many -SELECT id, blog_id, user_id, content, created_at +-- name: GetCommentsForPost :many +SELECT id, post_id, user_id, content, created_at FROM comments -WHERE blog_id = $1 +WHERE post_id = $1 ORDER BY created_at DESC; -- name: DeleteComment :exec diff --git a/db/query/featured_stories.sql b/db/query/featured_stories.sql index ec64f54..2471065 100644 --- a/db/query/featured_stories.sql +++ b/db/query/featured_stories.sql @@ -1,16 +1,16 @@ -- name: InsertFeaturedStory :one -INSERT INTO featured_stories (blog_id, featured_date) +INSERT INTO featured_stories (post_id, featured_date) VALUES ($1, $2) RETURNING *; -- name: GetFeaturedStory :one -SELECT id, blog_id, featured_date +SELECT id, post_id, featured_date FROM featured_stories WHERE id = $1; -- name: UpdateFeaturedStory :one UPDATE featured_stories -SET blog_id = $1, featured_date = $2 +SET post_id = $1, featured_date = $2 WHERE id = $3 RETURNING *; diff --git a/db/query/post_categories.sql b/db/query/post_categories.sql new file mode 100644 index 0000000..eee0358 --- /dev/null +++ b/db/query/post_categories.sql @@ -0,0 +1,14 @@ +-- name: InsertPostCategory :one +INSERT INTO post_categories (post_id, category_id) +VALUES ($1, $2) +RETURNING *; + +-- name: GetCategoriesForPost :many +SELECT c.id, c.name +FROM categories c +JOIN post_categories bc ON bc.category_id = c.id +WHERE bc.post_id = $1; + +-- name: DeletePostCategory :exec +DELETE FROM post_categories +WHERE post_id = $1 AND category_id = $2; \ No newline at end of file diff --git a/db/query/post_tags.sql b/db/query/post_tags.sql new file mode 100644 index 0000000..a5a33c9 --- /dev/null +++ b/db/query/post_tags.sql @@ -0,0 +1,14 @@ +-- name: InsertPostTag :one +INSERT INTO post_tags (post_id, tag_id) +VALUES ($1, $2) +RETURNING *; + +-- name: GetTagsForPost :many +SELECT t.id, t.name +FROM tags t +JOIN post_tags bt ON bt.tag_id = t.id +WHERE bt.post_id = $1; + +-- name: DeletePostTag :exec +DELETE FROM post_tags +WHERE post_id = $1 AND tag_id = $2; \ No newline at end of file diff --git a/db/query/blogs.sql b/db/query/posts.sql similarity index 72% rename from db/query/blogs.sql rename to db/query/posts.sql index c063bed..214e9b6 100644 --- a/db/query/blogs.sql +++ b/db/query/posts.sql @@ -1,20 +1,20 @@ --- name: InsertBlog :one -INSERT INTO blogs (user_id, title, summary, content, cover_image, created_at, updated_at, likes) +-- name: InsertPost :one +INSERT INTO posts (user_id, title, summary, content, cover_image, created_at, updated_at, likes) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING *; --- name: GetBlog :one +-- name: GetPost :one SELECT id, user_id, title, summary, content, cover_image, created_at, updated_at, likes -FROM blogs +FROM posts WHERE id = $1; --- name: GetBlogs :many +-- name: GetPosts :many SELECT id, user_id, title, summary, content, cover_image, created_at, updated_at, likes -FROM blogs +FROM posts ORDER BY id LIMIT $1 OFFSET $2; --- name: UpdateBlog :one -UPDATE blogs +-- name: UpdatePost :one +UPDATE posts SET title = COALESCE(sqlc.narg(title),title), summary = COALESCE(sqlc.narg(summary),summary), @@ -24,6 +24,6 @@ UPDATE blogs WHERE id = sqlc.arg(id) RETURNING *; --- name: DeleteBlog :exec -DELETE FROM blogs +-- name: DeletePost :exec +DELETE FROM posts WHERE id = $1; \ No newline at end of file diff --git a/db/query/profiles.sql b/db/query/profiles.sql index ee0c649..b2afb3e 100644 --- a/db/query/profiles.sql +++ b/db/query/profiles.sql @@ -1,10 +1,10 @@ -- name: InsertProfile :one -INSERT INTO profiles (user_id, bio, blog_count, like_count, follower_count) +INSERT INTO profiles (user_id, bio, post_count, like_count, follower_count) VALUES ($1, $2, $3, $4, $5) RETURNING *; -- name: GetProfile :one -SELECT user_id, bio, blog_count, like_count, follower_count +SELECT user_id, bio, post_count, like_count, follower_count FROM profiles WHERE user_id = $1; @@ -12,7 +12,7 @@ WHERE user_id = $1; UPDATE profiles SET bio = COALESCE(sqlc.narg(bio),bio), - blog_count = COALESCE(sqlc.narg(blog_count),blog_count), + post_count = COALESCE(sqlc.narg(post_count),post_count), like_count = COALESCE(sqlc.narg(like_count),like_count), follower_count = COALESCE(sqlc.narg(follower_count),follower_count) WHERE user_id = sqlc.arg(user_id) diff --git a/db/query/user_posts.sql b/db/query/user_posts.sql index aa0ac3a..5ddb8d4 100644 --- a/db/query/user_posts.sql +++ b/db/query/user_posts.sql @@ -1,20 +1,20 @@ -- name: InsertUserPost :one -INSERT INTO user_posts (user_id, blog_id) +INSERT INTO user_posts (user_id, post_id) VALUES ($1, $2) RETURNING *; -- name: DeleteUserPost :exec DELETE FROM user_posts -WHERE user_id = $1 AND blog_id = $2; +WHERE user_id = $1 AND post_id = $2; --- name: GetUserBlogs :many +-- name: GetUserPosts :many SELECT b.id, b.title, b.summary, b.content, b.cover_image, b.created_at, b.updated_at, b.likes -FROM blogs b -JOIN user_posts up ON up.blog_id = b.id +FROM posts b +JOIN user_posts up ON up.post_id = b.id WHERE up.user_id = $1; --- name: GetUserBlog :one +-- name: GetUserPost :one SELECT b.id, b.title, b.summary, b.content, b.cover_image, b.created_at, b.updated_at, b.likes -FROM blogs b -JOIN user_posts up ON up.blog_id = b.id +FROM posts b +JOIN user_posts up ON up.post_id = b.id WHERE up.user_id = $1 AND b.id = $2; \ No newline at end of file diff --git a/db/sqlc/blog_categories.sql.go b/db/sqlc/blog_categories.sql.go deleted file mode 100644 index 7fe00a3..0000000 --- a/db/sqlc/blog_categories.sql.go +++ /dev/null @@ -1,70 +0,0 @@ -// Code generated by sqlc. DO NOT EDIT. -// versions: -// sqlc v1.26.0 -// source: blog_categories.sql - -package db - -import ( - "context" -) - -const deleteBlogCategory = `-- name: DeleteBlogCategory :exec -DELETE FROM blog_categories -WHERE blog_id = $1 AND category_id = $2 -` - -type DeleteBlogCategoryParams struct { - BlogID int32 `json:"blog_id"` - CategoryID int32 `json:"category_id"` -} - -func (q *Queries) DeleteBlogCategory(ctx context.Context, arg DeleteBlogCategoryParams) error { - _, err := q.db.Exec(ctx, deleteBlogCategory, arg.BlogID, arg.CategoryID) - return err -} - -const getCategoriesForBlog = `-- name: GetCategoriesForBlog :many -SELECT c.id, c.name -FROM categories c -JOIN blog_categories bc ON bc.category_id = c.id -WHERE bc.blog_id = $1 -` - -func (q *Queries) GetCategoriesForBlog(ctx context.Context, blogID int32) ([]Category, error) { - rows, err := q.db.Query(ctx, getCategoriesForBlog, blogID) - if err != nil { - return nil, err - } - defer rows.Close() - items := []Category{} - for rows.Next() { - var i Category - if err := rows.Scan(&i.ID, &i.Name); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const insertBlogCategory = `-- name: InsertBlogCategory :one -INSERT INTO blog_categories (blog_id, category_id) -VALUES ($1, $2) -RETURNING blog_id, category_id -` - -type InsertBlogCategoryParams struct { - BlogID int32 `json:"blog_id"` - CategoryID int32 `json:"category_id"` -} - -func (q *Queries) InsertBlogCategory(ctx context.Context, arg InsertBlogCategoryParams) (BlogCategory, error) { - row := q.db.QueryRow(ctx, insertBlogCategory, arg.BlogID, arg.CategoryID) - var i BlogCategory - err := row.Scan(&i.BlogID, &i.CategoryID) - return i, err -} diff --git a/db/sqlc/blog_categories_test.go b/db/sqlc/blog_categories_test.go deleted file mode 100644 index 34b9ae5..0000000 --- a/db/sqlc/blog_categories_test.go +++ /dev/null @@ -1,40 +0,0 @@ -package db - -import ( - "context" - "testing" - - "github.com/stretchr/testify/require" -) - -func createRandomBlogCategory(t *testing.T) BlogCategory { - blog := createRandomBlog(t) - category := createRandomCategory(t) - arg := InsertBlogCategoryParams{ - BlogID: blog.ID, - CategoryID: category.ID, - } - - blogCategory, err := testStore.InsertBlogCategory(context.Background(), arg) - require.NoError(t, err) - require.NotEmpty(t, blogCategory) - return blogCategory -} - -func TestCreateBlogCategory(t *testing.T) { - createRandomBlogCategory(t) -} - -func TestGetBlogCategory(t *testing.T) { - randomBlogCategory := createRandomBlogCategory(t) - categories, err := testStore.GetCategoriesForBlog(context.Background(), randomBlogCategory.BlogID) - require.NoError(t, err) - require.NotEmpty(t, categories) -} - -func TestDeleteBlogCategory(t *testing.T) { - randomBlogCategory := createRandomBlogCategory(t) - categories, err := testStore.GetCategoriesForBlog(context.Background(), randomBlogCategory.BlogID) - require.NoError(t, err) - require.NotEmpty(t, categories) -} diff --git a/db/sqlc/blog_tags.sql.go b/db/sqlc/blog_tags.sql.go deleted file mode 100644 index dce05ec..0000000 --- a/db/sqlc/blog_tags.sql.go +++ /dev/null @@ -1,70 +0,0 @@ -// Code generated by sqlc. DO NOT EDIT. -// versions: -// sqlc v1.26.0 -// source: blog_tags.sql - -package db - -import ( - "context" -) - -const deleteBlogTag = `-- name: DeleteBlogTag :exec -DELETE FROM blog_tags -WHERE blog_id = $1 AND tag_id = $2 -` - -type DeleteBlogTagParams struct { - BlogID int32 `json:"blog_id"` - TagID int32 `json:"tag_id"` -} - -func (q *Queries) DeleteBlogTag(ctx context.Context, arg DeleteBlogTagParams) error { - _, err := q.db.Exec(ctx, deleteBlogTag, arg.BlogID, arg.TagID) - return err -} - -const getTagsForBlog = `-- name: GetTagsForBlog :many -SELECT t.id, t.name -FROM tags t -JOIN blog_tags bt ON bt.tag_id = t.id -WHERE bt.blog_id = $1 -` - -func (q *Queries) GetTagsForBlog(ctx context.Context, blogID int32) ([]Tag, error) { - rows, err := q.db.Query(ctx, getTagsForBlog, blogID) - if err != nil { - return nil, err - } - defer rows.Close() - items := []Tag{} - for rows.Next() { - var i Tag - if err := rows.Scan(&i.ID, &i.Name); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const insertBlogTag = `-- name: InsertBlogTag :one -INSERT INTO blog_tags (blog_id, tag_id) -VALUES ($1, $2) -RETURNING blog_id, tag_id -` - -type InsertBlogTagParams struct { - BlogID int32 `json:"blog_id"` - TagID int32 `json:"tag_id"` -} - -func (q *Queries) InsertBlogTag(ctx context.Context, arg InsertBlogTagParams) (BlogTag, error) { - row := q.db.QueryRow(ctx, insertBlogTag, arg.BlogID, arg.TagID) - var i BlogTag - err := row.Scan(&i.BlogID, &i.TagID) - return i, err -} diff --git a/db/sqlc/blog_tags_test.go b/db/sqlc/blog_tags_test.go deleted file mode 100644 index a49d33a..0000000 --- a/db/sqlc/blog_tags_test.go +++ /dev/null @@ -1,51 +0,0 @@ -package db - -import ( - "context" - "fmt" - "testing" - - "github.com/stretchr/testify/require" -) - -func createRandomBlogTag(t *testing.T, tagCount int) []BlogTag { - var blogTags []BlogTag - randomBlog := createRandomBlog(t) - - for i := 0; i < tagCount; i++ { - blogTag, err := testStore.InsertBlogTag(context.Background(), InsertBlogTagParams{ - BlogID: randomBlog.ID, - TagID: createRandomTag(t).ID, - }) - require.NoError(t, err) - require.NotEmpty(t, blogTag) - blogTags = append(blogTags, blogTag) - - } - return blogTags -} - -func TestCreateBlogTag(t *testing.T) { - createRandomBlogTag(t, 1) -} - -func TestGetBlogTag(t *testing.T) { - randomBlogTag := createRandomBlogTag(t, 1) - tags, err := testStore.GetTagsForBlog(context.Background(), randomBlogTag[0].BlogID) - require.NoError(t, err) - require.NotEmpty(t, tags) -} - -func TestDeleteBlogTag(t *testing.T) { - randomBlogTag := createRandomBlogTag(t, 1) - tags, err := testStore.GetTagsForBlog(context.Background(), randomBlogTag[0].BlogID) - require.NoError(t, err) - require.NotEmpty(t, tags) - fmt.Println(tags) - err = testStore.DeleteBlogTag(context.Background(), DeleteBlogTagParams{ - BlogID: randomBlogTag[0].BlogID, - TagID: tags[len(tags)-1].ID, - }) - fmt.Println(testStore.GetTagsForBlog(context.Background(), randomBlogTag[0].BlogID)) - require.NoError(t, err) -} diff --git a/db/sqlc/blogs_test.go b/db/sqlc/blogs_test.go deleted file mode 100644 index 20eeac4..0000000 --- a/db/sqlc/blogs_test.go +++ /dev/null @@ -1,207 +0,0 @@ -package db - -import ( - "context" - "testing" - - "github.com/jackc/pgx/v5/pgtype" - "github.com/mustafayilmazdev/musarchive/util" - - "github.com/stretchr/testify/require" -) - -func createRandomBlog(t *testing.T) Blog { - user := createRandomUser(t) - arg := InsertBlogParams{ - UserID: pgtype.Int4{ - Valid: true, - Int32: user.ID, - }, - Title: util.RandomTitle(), - Summary: util.RandomString(15), - Content: util.RandomDescription(), - CoverImage: pgtype.Text{ - Valid: true, - String: util.RandomImage(), - }, - } - blog, err := testStore.InsertBlog(context.Background(), arg) - require.NoError(t, err) - require.NotEmpty(t, blog) - - require.Equal(t, arg.UserID, blog.UserID) - require.Equal(t, arg.Title, blog.Title) - require.Equal(t, arg.Summary, blog.Summary) - require.Equal(t, arg.Content, blog.Content) - require.Equal(t, arg.Likes, blog.Likes) - require.Equal(t, arg.CreatedAt, blog.CreatedAt) - require.Equal(t, arg.UpdatedAt, blog.UpdatedAt) - - return blog -} - -func TestCreateBlog(t *testing.T) { - createRandomBlog(t) -} - -func TestGetBlog(t *testing.T) { - randomBlog := createRandomBlog(t) - blog, err := testStore.GetBlog(context.Background(), randomBlog.ID) - require.NoError(t, err) - require.NotEmpty(t, blog) - - require.Equal(t, randomBlog.UserID, blog.UserID) - require.Equal(t, randomBlog.Title, blog.Title) - require.Equal(t, randomBlog.Summary, blog.Summary) - require.Equal(t, randomBlog.Content, blog.Content) - require.Equal(t, randomBlog.Likes, blog.Likes) - require.Equal(t, randomBlog.CreatedAt, blog.CreatedAt) - require.Equal(t, randomBlog.UpdatedAt, blog.UpdatedAt) - -} - -func TestUpdateBlogTitle(t *testing.T) { - randomBlog := createRandomBlog(t) - updateBlog := UpdateBlogParams{ - ID: randomBlog.ID, - Title: pgtype.Text{ - Valid: true, - String: util.RandomTitle(), - }, - } - blog, err := testStore.UpdateBlog(context.Background(), updateBlog) - require.NoError(t, err) - require.NotEmpty(t, blog) - require.Equal(t, randomBlog.UserID, blog.UserID) - require.NotEqual(t, randomBlog.Title, blog.Title) - require.Equal(t, randomBlog.Summary, blog.Summary) - require.Equal(t, randomBlog.Content, blog.Content) - require.Equal(t, randomBlog.Likes, blog.Likes) - require.Equal(t, randomBlog.CreatedAt, blog.CreatedAt) - require.Equal(t, randomBlog.UpdatedAt, blog.UpdatedAt) -} - -func TestUpdateBlogSummary(t *testing.T) { - randomBlog := createRandomBlog(t) - updateBlog := UpdateBlogParams{ - ID: randomBlog.ID, - Summary: pgtype.Text{ - Valid: true, - String: util.RandomString(10), - }, - } - blog, err := testStore.UpdateBlog(context.Background(), updateBlog) - require.NoError(t, err) - require.NotEmpty(t, blog) - require.Equal(t, randomBlog.UserID, blog.UserID) - require.Equal(t, randomBlog.Title, blog.Title) - require.NotEqual(t, randomBlog.Summary, blog.Summary) - require.Equal(t, randomBlog.Content, blog.Content) - require.Equal(t, randomBlog.Likes, blog.Likes) - require.Equal(t, randomBlog.CreatedAt, blog.CreatedAt) - require.Equal(t, randomBlog.UpdatedAt, blog.UpdatedAt) -} - -func TestUpdateBlogContent(t *testing.T) { - randomBlog := createRandomBlog(t) - updateBlog := UpdateBlogParams{ - ID: randomBlog.ID, - Content: pgtype.Text{ - Valid: true, - String: util.RandomDescription(), - }, - } - blog, err := testStore.UpdateBlog(context.Background(), updateBlog) - require.NoError(t, err) - require.NotEmpty(t, blog) - - require.NotEqual(t, randomBlog.Content, blog.Content) - require.Equal(t, updateBlog.Content.String, blog.Content) - - require.Equal(t, randomBlog.UserID, blog.UserID) - require.Equal(t, randomBlog.Title, blog.Title) - require.Equal(t, randomBlog.Summary, blog.Summary) - require.Equal(t, randomBlog.Likes, blog.Likes) - require.Equal(t, randomBlog.CreatedAt, blog.CreatedAt) - require.Equal(t, randomBlog.UpdatedAt, blog.UpdatedAt) -} - -func TestUpdateBlogCover(t *testing.T) { - randomBlog := createRandomBlog(t) - updateBlog := UpdateBlogParams{ - ID: randomBlog.ID, - CoverImage: pgtype.Text{ - Valid: true, - String: util.RandomImage() + "/update", - }, - } - blog, err := testStore.UpdateBlog(context.Background(), updateBlog) - require.NoError(t, err) - require.NotEmpty(t, blog) - - require.NotEqual(t, randomBlog.CoverImage, blog.CoverImage) - require.Equal(t, updateBlog.CoverImage, blog.CoverImage) - - require.Equal(t, randomBlog.UserID, blog.UserID) - require.Equal(t, randomBlog.Title, blog.Title) - require.Equal(t, randomBlog.Content, blog.Content) - require.Equal(t, randomBlog.Likes, blog.Likes) - require.Equal(t, randomBlog.CreatedAt, blog.CreatedAt) - require.Equal(t, randomBlog.UpdatedAt, blog.UpdatedAt) -} - -func TestUpdateBlogAll(t *testing.T) { - randomBlog := createRandomBlog(t) - updateBlog := UpdateBlogParams{ - ID: randomBlog.ID, - Title: pgtype.Text{ - Valid: true, - String: util.RandomTitle(), - }, - Summary: pgtype.Text{ - Valid: true, - String: util.RandomString(10), - }, - Content: pgtype.Text{ - Valid: true, - String: util.RandomDescription(), - }, - Likes: pgtype.Int4{ - Valid: true, - Int32: util.RandomLike(), - }, - CoverImage: pgtype.Text{ - Valid: true, - String: util.RandomImage() + "/update", - }, - } - blog, err := testStore.UpdateBlog(context.Background(), updateBlog) - require.NoError(t, err) - require.NotEmpty(t, blog) - - require.NotEqual(t, randomBlog.CoverImage, blog.CoverImage) - require.NotEqual(t, randomBlog.Likes, blog.Likes) - require.NotEqual(t, randomBlog.Title, blog.Title) - require.NotEqual(t, randomBlog.Summary, blog.Summary) - require.NotEqual(t, randomBlog.Content, blog.Content) - - require.Equal(t, updateBlog.Title.String, blog.Title) - require.Equal(t, updateBlog.Summary.String, blog.Summary) - require.Equal(t, updateBlog.Content.String, blog.Content) - require.Equal(t, updateBlog.Likes, blog.Likes) - require.Equal(t, updateBlog.CoverImage, blog.CoverImage) - - require.Equal(t, randomBlog.UserID, blog.UserID) - require.Equal(t, randomBlog.CreatedAt, blog.CreatedAt) - require.Equal(t, randomBlog.UpdatedAt, blog.UpdatedAt) -} - -func TestDeleteBlog(t *testing.T) { - randomcategory := createRandomCategory(t) - err := testStore.DeleteCategory(context.Background(), randomcategory.ID) - require.NoError(t, err) - category, err := testStore.GetCategory(context.Background(), randomcategory.ID) - require.Error(t, err) - require.EqualError(t, err, ErrRecordNotFound.Error()) - require.Empty(t, category) -} diff --git a/db/sqlc/comments.sql.go b/db/sqlc/comments.sql.go index a4ef219..3acd0a1 100644 --- a/db/sqlc/comments.sql.go +++ b/db/sqlc/comments.sql.go @@ -21,15 +21,15 @@ func (q *Queries) DeleteComment(ctx context.Context, id int32) error { return err } -const getCommentsForBlog = `-- name: GetCommentsForBlog :many -SELECT id, blog_id, user_id, content, created_at +const getCommentsForPost = `-- name: GetCommentsForPost :many +SELECT id, post_id, user_id, content, created_at FROM comments -WHERE blog_id = $1 +WHERE post_id = $1 ORDER BY created_at DESC ` -func (q *Queries) GetCommentsForBlog(ctx context.Context, blogID int32) ([]Comment, error) { - rows, err := q.db.Query(ctx, getCommentsForBlog, blogID) +func (q *Queries) GetCommentsForPost(ctx context.Context, postID int32) ([]Comment, error) { + rows, err := q.db.Query(ctx, getCommentsForPost, postID) if err != nil { return nil, err } @@ -39,7 +39,7 @@ func (q *Queries) GetCommentsForBlog(ctx context.Context, blogID int32) ([]Comme var i Comment if err := rows.Scan( &i.ID, - &i.BlogID, + &i.PostID, &i.UserID, &i.Content, &i.CreatedAt, @@ -55,13 +55,13 @@ func (q *Queries) GetCommentsForBlog(ctx context.Context, blogID int32) ([]Comme } const insertComment = `-- name: InsertComment :one -INSERT INTO comments (blog_id, user_id, content, created_at) +INSERT INTO comments (post_id, user_id, content, created_at) VALUES ($1, $2, $3, $4) -RETURNING id, blog_id, user_id, content, created_at +RETURNING id, post_id, user_id, content, created_at ` type InsertCommentParams struct { - BlogID int32 `json:"blog_id"` + PostID int32 `json:"post_id"` UserID int32 `json:"user_id"` Content string `json:"content"` CreatedAt pgtype.Timestamptz `json:"created_at"` @@ -69,7 +69,7 @@ type InsertCommentParams struct { func (q *Queries) InsertComment(ctx context.Context, arg InsertCommentParams) (Comment, error) { row := q.db.QueryRow(ctx, insertComment, - arg.BlogID, + arg.PostID, arg.UserID, arg.Content, arg.CreatedAt, @@ -77,7 +77,7 @@ func (q *Queries) InsertComment(ctx context.Context, arg InsertCommentParams) (C var i Comment err := row.Scan( &i.ID, - &i.BlogID, + &i.PostID, &i.UserID, &i.Content, &i.CreatedAt, diff --git a/db/sqlc/comments_test.go b/db/sqlc/comments_test.go index f7b0a50..47c1488 100644 --- a/db/sqlc/comments_test.go +++ b/db/sqlc/comments_test.go @@ -9,16 +9,16 @@ import ( ) func createRandomComment(t *testing.T) Comment { - randomBlog := createRandomBlog(t) + randomBlog := createRandomPost(t) arg := InsertCommentParams{ - BlogID: randomBlog.ID, + PostID: randomBlog.ID, UserID: randomBlog.UserID.Int32, Content: util.RandomDescription(), } comment, err := testStore.InsertComment(context.Background(), arg) require.NoError(t, err) require.NotEmpty(t, comment) - require.Equal(t, comment.BlogID, arg.BlogID) + require.Equal(t, comment.PostID, arg.PostID) require.Equal(t, comment.UserID, arg.UserID) require.Equal(t, comment.Content, arg.Content) @@ -31,7 +31,7 @@ func TestCreateComment(t *testing.T) { func TestGetComments(t *testing.T) { randomComment := createRandomComment(t) - comments, err := testStore.GetCommentsForBlog(context.Background(), randomComment.BlogID) + comments, err := testStore.GetCommentsForPost(context.Background(), randomComment.PostID) require.NoError(t, err) require.NotEmpty(t, comments) } @@ -40,6 +40,6 @@ func TestDeleteComment(t *testing.T) { randomComment := createRandomComment(t) err := testStore.DeleteComment(context.Background(), randomComment.ID) require.NoError(t, err) - _, err = testStore.GetCommentsForBlog(context.Background(), randomComment.ID) + _, err = testStore.GetCommentsForPost(context.Background(), randomComment.ID) require.NoError(t, err) } diff --git a/db/sqlc/featured_stories.sql.go b/db/sqlc/featured_stories.sql.go index 61bffef..f3df5de 100644 --- a/db/sqlc/featured_stories.sql.go +++ b/db/sqlc/featured_stories.sql.go @@ -22,7 +22,7 @@ func (q *Queries) DeleteFeaturedStory(ctx context.Context, id int32) error { } const getFeaturedStory = `-- name: GetFeaturedStory :one -SELECT id, blog_id, featured_date +SELECT id, post_id, featured_date FROM featured_stories WHERE id = $1 ` @@ -30,44 +30,44 @@ WHERE id = $1 func (q *Queries) GetFeaturedStory(ctx context.Context, id int32) (FeaturedStory, error) { row := q.db.QueryRow(ctx, getFeaturedStory, id) var i FeaturedStory - err := row.Scan(&i.ID, &i.BlogID, &i.FeaturedDate) + err := row.Scan(&i.ID, &i.PostID, &i.FeaturedDate) return i, err } const insertFeaturedStory = `-- name: InsertFeaturedStory :one -INSERT INTO featured_stories (blog_id, featured_date) +INSERT INTO featured_stories (post_id, featured_date) VALUES ($1, $2) -RETURNING id, blog_id, featured_date +RETURNING id, post_id, featured_date ` type InsertFeaturedStoryParams struct { - BlogID int32 `json:"blog_id"` + PostID int32 `json:"post_id"` FeaturedDate pgtype.Date `json:"featured_date"` } func (q *Queries) InsertFeaturedStory(ctx context.Context, arg InsertFeaturedStoryParams) (FeaturedStory, error) { - row := q.db.QueryRow(ctx, insertFeaturedStory, arg.BlogID, arg.FeaturedDate) + row := q.db.QueryRow(ctx, insertFeaturedStory, arg.PostID, arg.FeaturedDate) var i FeaturedStory - err := row.Scan(&i.ID, &i.BlogID, &i.FeaturedDate) + err := row.Scan(&i.ID, &i.PostID, &i.FeaturedDate) return i, err } const updateFeaturedStory = `-- name: UpdateFeaturedStory :one UPDATE featured_stories -SET blog_id = $1, featured_date = $2 +SET post_id = $1, featured_date = $2 WHERE id = $3 -RETURNING id, blog_id, featured_date +RETURNING id, post_id, featured_date ` type UpdateFeaturedStoryParams struct { - BlogID int32 `json:"blog_id"` + PostID int32 `json:"post_id"` FeaturedDate pgtype.Date `json:"featured_date"` ID int32 `json:"id"` } func (q *Queries) UpdateFeaturedStory(ctx context.Context, arg UpdateFeaturedStoryParams) (FeaturedStory, error) { - row := q.db.QueryRow(ctx, updateFeaturedStory, arg.BlogID, arg.FeaturedDate, arg.ID) + row := q.db.QueryRow(ctx, updateFeaturedStory, arg.PostID, arg.FeaturedDate, arg.ID) var i FeaturedStory - err := row.Scan(&i.ID, &i.BlogID, &i.FeaturedDate) + err := row.Scan(&i.ID, &i.PostID, &i.FeaturedDate) return i, err } diff --git a/db/sqlc/featured_stories_test.go b/db/sqlc/featured_stories_test.go index bbbaea2..1dbe30f 100644 --- a/db/sqlc/featured_stories_test.go +++ b/db/sqlc/featured_stories_test.go @@ -11,9 +11,9 @@ import ( ) func createRandomFeaturedStory(t *testing.T) FeaturedStory { - randomBlog := createRandomBlog(t) + randomBlog := createRandomPost(t) arg := InsertFeaturedStoryParams{ - BlogID: randomBlog.ID, + PostID: randomBlog.ID, FeaturedDate: pgtype.Date{ Valid: true, Time: util.DateNow(), @@ -23,7 +23,7 @@ func createRandomFeaturedStory(t *testing.T) FeaturedStory { require.NoError(t, err) require.NotEmpty(t, featuredStory) - require.Equal(t, arg.BlogID, featuredStory.BlogID) + require.Equal(t, arg.PostID, featuredStory.PostID) require.Equal(t, arg.FeaturedDate.Time, featuredStory.FeaturedDate.Time) return featuredStory @@ -39,17 +39,17 @@ func TestGetFeaturedStory(t *testing.T) { require.NoError(t, err) require.NotEmpty(t, featuredStory) require.Equal(t, randomFeaturedStory.ID, featuredStory.ID) - require.Equal(t, randomFeaturedStory.BlogID, featuredStory.BlogID) + require.Equal(t, randomFeaturedStory.PostID, featuredStory.PostID) require.Equal(t, randomFeaturedStory.FeaturedDate, featuredStory.FeaturedDate) } func TestUpdateFeaturedStory(t *testing.T) { randomFeaturedStory := createRandomFeaturedStory(t) - randomBlog := createRandomBlog(t) + randomBlog := createRandomPost(t) updateFeaturedStory := UpdateFeaturedStoryParams{ ID: randomFeaturedStory.ID, - BlogID: randomBlog.ID, + PostID: randomBlog.ID, FeaturedDate: pgtype.Date{ Valid: true, Time: util.DateYesterday(), @@ -60,7 +60,7 @@ func TestUpdateFeaturedStory(t *testing.T) { require.NoError(t, err) require.NotEmpty(t, featuredStory) require.Equal(t, updateFeaturedStory.ID, featuredStory.ID) - require.Equal(t, updateFeaturedStory.BlogID, featuredStory.BlogID) + require.Equal(t, updateFeaturedStory.PostID, featuredStory.PostID) require.Equal(t, updateFeaturedStory.FeaturedDate, featuredStory.FeaturedDate) require.NotEqual(t, randomFeaturedStory.FeaturedDate, featuredStory.FeaturedDate) diff --git a/db/sqlc/models.go b/db/sqlc/models.go index b294117..5952f63 100644 --- a/db/sqlc/models.go +++ b/db/sqlc/models.go @@ -11,33 +11,6 @@ import ( "github.com/jackc/pgx/v5/pgtype" ) -type Blog struct { - ID int32 `json:"id"` - UserID pgtype.Int4 `json:"user_id"` - Title string `json:"title"` - Summary string `json:"summary"` - Content string `json:"content"` - CoverImage pgtype.Text `json:"cover_image"` - CreatedAt pgtype.Timestamptz `json:"created_at"` - UpdatedAt pgtype.Timestamptz `json:"updated_at"` - Likes pgtype.Int4 `json:"likes"` -} - -type BlogCategory struct { - BlogID int32 `json:"blog_id"` - CategoryID int32 `json:"category_id"` -} - -type BlogLike struct { - BlogID int32 `json:"blog_id"` - UserID int32 `json:"user_id"` -} - -type BlogTag struct { - BlogID int32 `json:"blog_id"` - TagID int32 `json:"tag_id"` -} - type Category struct { ID int32 `json:"id"` Name string `json:"name"` @@ -45,7 +18,7 @@ type Category struct { type Comment struct { ID int32 `json:"id"` - BlogID int32 `json:"blog_id"` + PostID int32 `json:"post_id"` UserID int32 `json:"user_id"` Content string `json:"content"` CreatedAt pgtype.Timestamptz `json:"created_at"` @@ -53,7 +26,7 @@ type Comment struct { type FeaturedStory struct { ID int32 `json:"id"` - BlogID int32 `json:"blog_id"` + PostID int32 `json:"post_id"` FeaturedDate pgtype.Date `json:"featured_date"` } @@ -64,10 +37,37 @@ type Onboarding struct { Description string `json:"description"` } +type Post struct { + ID int32 `json:"id"` + UserID pgtype.Int4 `json:"user_id"` + Title string `json:"title"` + Summary string `json:"summary"` + Content string `json:"content"` + CoverImage pgtype.Text `json:"cover_image"` + CreatedAt pgtype.Timestamptz `json:"created_at"` + UpdatedAt pgtype.Timestamptz `json:"updated_at"` + Likes pgtype.Int4 `json:"likes"` +} + +type PostCategory struct { + PostID int32 `json:"post_id"` + CategoryID int32 `json:"category_id"` +} + +type PostLike struct { + PostID int32 `json:"post_id"` + UserID int32 `json:"user_id"` +} + +type PostTag struct { + PostID int32 `json:"post_id"` + TagID int32 `json:"tag_id"` +} + type Profile struct { UserID int32 `json:"user_id"` Bio pgtype.Text `json:"bio"` - BlogCount pgtype.Int4 `json:"blog_count"` + PostCount pgtype.Int4 `json:"post_count"` LikeCount pgtype.Int4 `json:"like_count"` FollowerCount pgtype.Int4 `json:"follower_count"` } @@ -108,5 +108,5 @@ type UserFollower struct { type UserPost struct { UserID int32 `json:"user_id"` - BlogID int32 `json:"blog_id"` + PostID int32 `json:"post_id"` } diff --git a/db/sqlc/post_categories.sql.go b/db/sqlc/post_categories.sql.go new file mode 100644 index 0000000..5c782e5 --- /dev/null +++ b/db/sqlc/post_categories.sql.go @@ -0,0 +1,70 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.26.0 +// source: post_categories.sql + +package db + +import ( + "context" +) + +const deletePostCategory = `-- name: DeletePostCategory :exec +DELETE FROM post_categories +WHERE post_id = $1 AND category_id = $2 +` + +type DeletePostCategoryParams struct { + PostID int32 `json:"post_id"` + CategoryID int32 `json:"category_id"` +} + +func (q *Queries) DeletePostCategory(ctx context.Context, arg DeletePostCategoryParams) error { + _, err := q.db.Exec(ctx, deletePostCategory, arg.PostID, arg.CategoryID) + return err +} + +const getCategoriesForPost = `-- name: GetCategoriesForPost :many +SELECT c.id, c.name +FROM categories c +JOIN post_categories bc ON bc.category_id = c.id +WHERE bc.post_id = $1 +` + +func (q *Queries) GetCategoriesForPost(ctx context.Context, postID int32) ([]Category, error) { + rows, err := q.db.Query(ctx, getCategoriesForPost, postID) + if err != nil { + return nil, err + } + defer rows.Close() + items := []Category{} + for rows.Next() { + var i Category + if err := rows.Scan(&i.ID, &i.Name); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const insertPostCategory = `-- name: InsertPostCategory :one +INSERT INTO post_categories (post_id, category_id) +VALUES ($1, $2) +RETURNING post_id, category_id +` + +type InsertPostCategoryParams struct { + PostID int32 `json:"post_id"` + CategoryID int32 `json:"category_id"` +} + +func (q *Queries) InsertPostCategory(ctx context.Context, arg InsertPostCategoryParams) (PostCategory, error) { + row := q.db.QueryRow(ctx, insertPostCategory, arg.PostID, arg.CategoryID) + var i PostCategory + err := row.Scan(&i.PostID, &i.CategoryID) + return i, err +} diff --git a/db/sqlc/post_categories_test.go b/db/sqlc/post_categories_test.go new file mode 100644 index 0000000..77842f0 --- /dev/null +++ b/db/sqlc/post_categories_test.go @@ -0,0 +1,40 @@ +package db + +import ( + "context" + "testing" + + "github.com/stretchr/testify/require" +) + +func createRandomPostCategory(t *testing.T) PostCategory { + post := createRandomUserPost(t) + category := createRandomCategory(t) + arg := InsertPostCategoryParams{ + PostID: post.PostID, + CategoryID: category.ID, + } + + postCategory, err := testStore.InsertPostCategory(context.Background(), arg) + require.NoError(t, err) + require.NotEmpty(t, postCategory) + return postCategory +} + +func TestCreatepostCategory(t *testing.T) { + createRandomPostCategory(t) +} + +func TestGetpostCategory(t *testing.T) { + randompostCategory := createRandomPostCategory(t) + categories, err := testStore.GetCategoriesForPost(context.Background(), randompostCategory.PostID) + require.NoError(t, err) + require.NotEmpty(t, categories) +} + +func TestDeletepostCategory(t *testing.T) { + randompostCategory := createRandomPostCategory(t) + categories, err := testStore.GetCategoriesForPost(context.Background(), randompostCategory.PostID) + require.NoError(t, err) + require.NotEmpty(t, categories) +} diff --git a/db/sqlc/post_tags.sql.go b/db/sqlc/post_tags.sql.go new file mode 100644 index 0000000..36415bc --- /dev/null +++ b/db/sqlc/post_tags.sql.go @@ -0,0 +1,70 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.26.0 +// source: post_tags.sql + +package db + +import ( + "context" +) + +const deletePostTag = `-- name: DeletePostTag :exec +DELETE FROM post_tags +WHERE post_id = $1 AND tag_id = $2 +` + +type DeletePostTagParams struct { + PostID int32 `json:"post_id"` + TagID int32 `json:"tag_id"` +} + +func (q *Queries) DeletePostTag(ctx context.Context, arg DeletePostTagParams) error { + _, err := q.db.Exec(ctx, deletePostTag, arg.PostID, arg.TagID) + return err +} + +const getTagsForPost = `-- name: GetTagsForPost :many +SELECT t.id, t.name +FROM tags t +JOIN post_tags bt ON bt.tag_id = t.id +WHERE bt.post_id = $1 +` + +func (q *Queries) GetTagsForPost(ctx context.Context, postID int32) ([]Tag, error) { + rows, err := q.db.Query(ctx, getTagsForPost, postID) + if err != nil { + return nil, err + } + defer rows.Close() + items := []Tag{} + for rows.Next() { + var i Tag + if err := rows.Scan(&i.ID, &i.Name); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const insertPostTag = `-- name: InsertPostTag :one +INSERT INTO post_tags (post_id, tag_id) +VALUES ($1, $2) +RETURNING post_id, tag_id +` + +type InsertPostTagParams struct { + PostID int32 `json:"post_id"` + TagID int32 `json:"tag_id"` +} + +func (q *Queries) InsertPostTag(ctx context.Context, arg InsertPostTagParams) (PostTag, error) { + row := q.db.QueryRow(ctx, insertPostTag, arg.PostID, arg.TagID) + var i PostTag + err := row.Scan(&i.PostID, &i.TagID) + return i, err +} diff --git a/db/sqlc/post_tags_test.go b/db/sqlc/post_tags_test.go new file mode 100644 index 0000000..68da8d1 --- /dev/null +++ b/db/sqlc/post_tags_test.go @@ -0,0 +1,51 @@ +package db + +import ( + "context" + "fmt" + "testing" + + "github.com/stretchr/testify/require" +) + +func createRandomPostTag(t *testing.T, tagCount int) []PostTag { + var postTags []PostTag + randomPost := createRandomPost(t) + + for i := 0; i < tagCount; i++ { + PostTag, err := testStore.InsertPostTag(context.Background(), InsertPostTagParams{ + PostID: randomPost.ID, + TagID: createRandomTag(t).ID, + }) + require.NoError(t, err) + require.NotEmpty(t, PostTag) + postTags = append(postTags, PostTag) + + } + return postTags +} + +func TestCreatePostTag(t *testing.T) { + createRandomPostTag(t, 1) +} + +func TestGetPostTag(t *testing.T) { + randomPostTag := createRandomPostTag(t, 1) + tags, err := testStore.GetTagsForPost(context.Background(), randomPostTag[0].PostID) + require.NoError(t, err) + require.NotEmpty(t, tags) +} + +func TestDeletePostTag(t *testing.T) { + randomPostTag := createRandomPostTag(t, 1) + tags, err := testStore.GetTagsForPost(context.Background(), randomPostTag[0].PostID) + require.NoError(t, err) + require.NotEmpty(t, tags) + fmt.Println(tags) + err = testStore.DeletePostTag(context.Background(), DeletePostTagParams{ + PostID: randomPostTag[0].PostID, + TagID: tags[len(tags)-1].ID, + }) + fmt.Println(testStore.GetTagsForPost(context.Background(), randomPostTag[0].PostID)) + require.NoError(t, err) +} diff --git a/db/sqlc/blogs.sql.go b/db/sqlc/posts.sql.go similarity index 70% rename from db/sqlc/blogs.sql.go rename to db/sqlc/posts.sql.go index 56cbed0..d7a72ef 100644 --- a/db/sqlc/blogs.sql.go +++ b/db/sqlc/posts.sql.go @@ -1,7 +1,7 @@ // Code generated by sqlc. DO NOT EDIT. // versions: // sqlc v1.26.0 -// source: blogs.sql +// source: posts.sql package db @@ -11,25 +11,25 @@ import ( "github.com/jackc/pgx/v5/pgtype" ) -const deleteBlog = `-- name: DeleteBlog :exec -DELETE FROM blogs +const deletePost = `-- name: DeletePost :exec +DELETE FROM posts WHERE id = $1 ` -func (q *Queries) DeleteBlog(ctx context.Context, id int32) error { - _, err := q.db.Exec(ctx, deleteBlog, id) +func (q *Queries) DeletePost(ctx context.Context, id int32) error { + _, err := q.db.Exec(ctx, deletePost, id) return err } -const getBlog = `-- name: GetBlog :one +const getPost = `-- name: GetPost :one SELECT id, user_id, title, summary, content, cover_image, created_at, updated_at, likes -FROM blogs +FROM posts WHERE id = $1 ` -func (q *Queries) GetBlog(ctx context.Context, id int32) (Blog, error) { - row := q.db.QueryRow(ctx, getBlog, id) - var i Blog +func (q *Queries) GetPost(ctx context.Context, id int32) (Post, error) { + row := q.db.QueryRow(ctx, getPost, id) + var i Post err := row.Scan( &i.ID, &i.UserID, @@ -44,26 +44,26 @@ func (q *Queries) GetBlog(ctx context.Context, id int32) (Blog, error) { return i, err } -const getBlogs = `-- name: GetBlogs :many +const getPosts = `-- name: GetPosts :many SELECT id, user_id, title, summary, content, cover_image, created_at, updated_at, likes -FROM blogs +FROM posts ORDER BY id LIMIT $1 OFFSET $2 ` -type GetBlogsParams struct { +type GetPostsParams struct { Limit int32 `json:"limit"` Offset int32 `json:"offset"` } -func (q *Queries) GetBlogs(ctx context.Context, arg GetBlogsParams) ([]Blog, error) { - rows, err := q.db.Query(ctx, getBlogs, arg.Limit, arg.Offset) +func (q *Queries) GetPosts(ctx context.Context, arg GetPostsParams) ([]Post, error) { + rows, err := q.db.Query(ctx, getPosts, arg.Limit, arg.Offset) if err != nil { return nil, err } defer rows.Close() - items := []Blog{} + items := []Post{} for rows.Next() { - var i Blog + var i Post if err := rows.Scan( &i.ID, &i.UserID, @@ -85,13 +85,13 @@ func (q *Queries) GetBlogs(ctx context.Context, arg GetBlogsParams) ([]Blog, err return items, nil } -const insertBlog = `-- name: InsertBlog :one -INSERT INTO blogs (user_id, title, summary, content, cover_image, created_at, updated_at, likes) +const insertPost = `-- name: InsertPost :one +INSERT INTO posts (user_id, title, summary, content, cover_image, created_at, updated_at, likes) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING id, user_id, title, summary, content, cover_image, created_at, updated_at, likes ` -type InsertBlogParams struct { +type InsertPostParams struct { UserID pgtype.Int4 `json:"user_id"` Title string `json:"title"` Summary string `json:"summary"` @@ -102,8 +102,8 @@ type InsertBlogParams struct { Likes pgtype.Int4 `json:"likes"` } -func (q *Queries) InsertBlog(ctx context.Context, arg InsertBlogParams) (Blog, error) { - row := q.db.QueryRow(ctx, insertBlog, +func (q *Queries) InsertPost(ctx context.Context, arg InsertPostParams) (Post, error) { + row := q.db.QueryRow(ctx, insertPost, arg.UserID, arg.Title, arg.Summary, @@ -113,7 +113,7 @@ func (q *Queries) InsertBlog(ctx context.Context, arg InsertBlogParams) (Blog, e arg.UpdatedAt, arg.Likes, ) - var i Blog + var i Post err := row.Scan( &i.ID, &i.UserID, @@ -128,8 +128,8 @@ func (q *Queries) InsertBlog(ctx context.Context, arg InsertBlogParams) (Blog, e return i, err } -const updateBlog = `-- name: UpdateBlog :one -UPDATE blogs +const updatePost = `-- name: UpdatePost :one +UPDATE posts SET title = COALESCE($1,title), summary = COALESCE($2,summary), @@ -140,7 +140,7 @@ UPDATE blogs RETURNING id, user_id, title, summary, content, cover_image, created_at, updated_at, likes ` -type UpdateBlogParams struct { +type UpdatePostParams struct { Title pgtype.Text `json:"title"` Summary pgtype.Text `json:"summary"` Content pgtype.Text `json:"content"` @@ -149,8 +149,8 @@ type UpdateBlogParams struct { ID int32 `json:"id"` } -func (q *Queries) UpdateBlog(ctx context.Context, arg UpdateBlogParams) (Blog, error) { - row := q.db.QueryRow(ctx, updateBlog, +func (q *Queries) UpdatePost(ctx context.Context, arg UpdatePostParams) (Post, error) { + row := q.db.QueryRow(ctx, updatePost, arg.Title, arg.Summary, arg.Content, @@ -158,7 +158,7 @@ func (q *Queries) UpdateBlog(ctx context.Context, arg UpdateBlogParams) (Blog, e arg.Likes, arg.ID, ) - var i Blog + var i Post err := row.Scan( &i.ID, &i.UserID, diff --git a/db/sqlc/posts_test.go b/db/sqlc/posts_test.go new file mode 100644 index 0000000..cadc9db --- /dev/null +++ b/db/sqlc/posts_test.go @@ -0,0 +1,207 @@ +package db + +import ( + "context" + "testing" + + "github.com/jackc/pgx/v5/pgtype" + "github.com/mustafayilmazdev/musarchive/util" + + "github.com/stretchr/testify/require" +) + +func createRandomPost(t *testing.T) Post { + user := createRandomUser(t) + arg := InsertPostParams{ + UserID: pgtype.Int4{ + Valid: true, + Int32: user.ID, + }, + Title: util.RandomTitle(), + Summary: util.RandomString(15), + Content: util.RandomDescription(), + CoverImage: pgtype.Text{ + Valid: true, + String: util.RandomImage(), + }, + } + post, err := testStore.InsertPost(context.Background(), arg) + require.NoError(t, err) + require.NotEmpty(t, post) + + require.Equal(t, arg.UserID, post.UserID) + require.Equal(t, arg.Title, post.Title) + require.Equal(t, arg.Summary, post.Summary) + require.Equal(t, arg.Content, post.Content) + require.Equal(t, arg.Likes, post.Likes) + require.Equal(t, arg.CreatedAt, post.CreatedAt) + require.Equal(t, arg.UpdatedAt, post.UpdatedAt) + + return post +} + +func TestCreateBlog(t *testing.T) { + createRandomPost(t) +} + +func TestGetBlog(t *testing.T) { + randomBlog := createRandomPost(t) + post, err := testStore.GetPost(context.Background(), randomBlog.ID) + require.NoError(t, err) + require.NotEmpty(t, post) + + require.Equal(t, randomBlog.UserID, post.UserID) + require.Equal(t, randomBlog.Title, post.Title) + require.Equal(t, randomBlog.Summary, post.Summary) + require.Equal(t, randomBlog.Content, post.Content) + require.Equal(t, randomBlog.Likes, post.Likes) + require.Equal(t, randomBlog.CreatedAt, post.CreatedAt) + require.Equal(t, randomBlog.UpdatedAt, post.UpdatedAt) + +} + +func TestUpdatePostTitle(t *testing.T) { + randomBlog := createRandomPost(t) + updatePost := UpdatePostParams{ + ID: randomBlog.ID, + Title: pgtype.Text{ + Valid: true, + String: util.RandomTitle(), + }, + } + post, err := testStore.UpdatePost(context.Background(), updatePost) + require.NoError(t, err) + require.NotEmpty(t, post) + require.Equal(t, randomBlog.UserID, post.UserID) + require.NotEqual(t, randomBlog.Title, post.Title) + require.Equal(t, randomBlog.Summary, post.Summary) + require.Equal(t, randomBlog.Content, post.Content) + require.Equal(t, randomBlog.Likes, post.Likes) + require.Equal(t, randomBlog.CreatedAt, post.CreatedAt) + require.Equal(t, randomBlog.UpdatedAt, post.UpdatedAt) +} + +func TestUpdatePostSummary(t *testing.T) { + randomBlog := createRandomPost(t) + updatePost := UpdatePostParams{ + ID: randomBlog.ID, + Summary: pgtype.Text{ + Valid: true, + String: util.RandomString(10), + }, + } + post, err := testStore.UpdatePost(context.Background(), updatePost) + require.NoError(t, err) + require.NotEmpty(t, post) + require.Equal(t, randomBlog.UserID, post.UserID) + require.Equal(t, randomBlog.Title, post.Title) + require.NotEqual(t, randomBlog.Summary, post.Summary) + require.Equal(t, randomBlog.Content, post.Content) + require.Equal(t, randomBlog.Likes, post.Likes) + require.Equal(t, randomBlog.CreatedAt, post.CreatedAt) + require.Equal(t, randomBlog.UpdatedAt, post.UpdatedAt) +} + +func TestUpdatePostContent(t *testing.T) { + randomBlog := createRandomPost(t) + updatePost := UpdatePostParams{ + ID: randomBlog.ID, + Content: pgtype.Text{ + Valid: true, + String: util.RandomDescription(), + }, + } + post, err := testStore.UpdatePost(context.Background(), updatePost) + require.NoError(t, err) + require.NotEmpty(t, post) + + require.NotEqual(t, randomBlog.Content, post.Content) + require.Equal(t, updatePost.Content.String, post.Content) + + require.Equal(t, randomBlog.UserID, post.UserID) + require.Equal(t, randomBlog.Title, post.Title) + require.Equal(t, randomBlog.Summary, post.Summary) + require.Equal(t, randomBlog.Likes, post.Likes) + require.Equal(t, randomBlog.CreatedAt, post.CreatedAt) + require.Equal(t, randomBlog.UpdatedAt, post.UpdatedAt) +} + +func TestUpdatePostCover(t *testing.T) { + randomBlog := createRandomPost(t) + updatePost := UpdatePostParams{ + ID: randomBlog.ID, + CoverImage: pgtype.Text{ + Valid: true, + String: util.RandomImage() + "/update", + }, + } + post, err := testStore.UpdatePost(context.Background(), updatePost) + require.NoError(t, err) + require.NotEmpty(t, post) + + require.NotEqual(t, randomBlog.CoverImage, post.CoverImage) + require.Equal(t, updatePost.CoverImage, post.CoverImage) + + require.Equal(t, randomBlog.UserID, post.UserID) + require.Equal(t, randomBlog.Title, post.Title) + require.Equal(t, randomBlog.Content, post.Content) + require.Equal(t, randomBlog.Likes, post.Likes) + require.Equal(t, randomBlog.CreatedAt, post.CreatedAt) + require.Equal(t, randomBlog.UpdatedAt, post.UpdatedAt) +} + +func TestUpdatePostAll(t *testing.T) { + randomBlog := createRandomPost(t) + updatePost := UpdatePostParams{ + ID: randomBlog.ID, + Title: pgtype.Text{ + Valid: true, + String: util.RandomTitle(), + }, + Summary: pgtype.Text{ + Valid: true, + String: util.RandomString(10), + }, + Content: pgtype.Text{ + Valid: true, + String: util.RandomDescription(), + }, + Likes: pgtype.Int4{ + Valid: true, + Int32: util.RandomLike(), + }, + CoverImage: pgtype.Text{ + Valid: true, + String: util.RandomImage() + "/update", + }, + } + post, err := testStore.UpdatePost(context.Background(), updatePost) + require.NoError(t, err) + require.NotEmpty(t, post) + + require.NotEqual(t, randomBlog.CoverImage, post.CoverImage) + require.NotEqual(t, randomBlog.Likes, post.Likes) + require.NotEqual(t, randomBlog.Title, post.Title) + require.NotEqual(t, randomBlog.Summary, post.Summary) + require.NotEqual(t, randomBlog.Content, post.Content) + + require.Equal(t, updatePost.Title.String, post.Title) + require.Equal(t, updatePost.Summary.String, post.Summary) + require.Equal(t, updatePost.Content.String, post.Content) + require.Equal(t, updatePost.Likes, post.Likes) + require.Equal(t, updatePost.CoverImage, post.CoverImage) + + require.Equal(t, randomBlog.UserID, post.UserID) + require.Equal(t, randomBlog.CreatedAt, post.CreatedAt) + require.Equal(t, randomBlog.UpdatedAt, post.UpdatedAt) +} + +func TestDeleteBlog(t *testing.T) { + randomcategory := createRandomCategory(t) + err := testStore.DeleteCategory(context.Background(), randomcategory.ID) + require.NoError(t, err) + category, err := testStore.GetCategory(context.Background(), randomcategory.ID) + require.Error(t, err) + require.EqualError(t, err, ErrRecordNotFound.Error()) + require.Empty(t, category) +} diff --git a/db/sqlc/profiles.sql.go b/db/sqlc/profiles.sql.go index e093918..dcc3ae2 100644 --- a/db/sqlc/profiles.sql.go +++ b/db/sqlc/profiles.sql.go @@ -22,7 +22,7 @@ func (q *Queries) DeleteProfile(ctx context.Context, userID int32) error { } const getProfile = `-- name: GetProfile :one -SELECT user_id, bio, blog_count, like_count, follower_count +SELECT user_id, bio, post_count, like_count, follower_count FROM profiles WHERE user_id = $1 ` @@ -33,7 +33,7 @@ func (q *Queries) GetProfile(ctx context.Context, userID int32) (Profile, error) err := row.Scan( &i.UserID, &i.Bio, - &i.BlogCount, + &i.PostCount, &i.LikeCount, &i.FollowerCount, ) @@ -41,15 +41,15 @@ func (q *Queries) GetProfile(ctx context.Context, userID int32) (Profile, error) } const insertProfile = `-- name: InsertProfile :one -INSERT INTO profiles (user_id, bio, blog_count, like_count, follower_count) +INSERT INTO profiles (user_id, bio, post_count, like_count, follower_count) VALUES ($1, $2, $3, $4, $5) -RETURNING user_id, bio, blog_count, like_count, follower_count +RETURNING user_id, bio, post_count, like_count, follower_count ` type InsertProfileParams struct { UserID int32 `json:"user_id"` Bio pgtype.Text `json:"bio"` - BlogCount pgtype.Int4 `json:"blog_count"` + PostCount pgtype.Int4 `json:"post_count"` LikeCount pgtype.Int4 `json:"like_count"` FollowerCount pgtype.Int4 `json:"follower_count"` } @@ -58,7 +58,7 @@ func (q *Queries) InsertProfile(ctx context.Context, arg InsertProfileParams) (P row := q.db.QueryRow(ctx, insertProfile, arg.UserID, arg.Bio, - arg.BlogCount, + arg.PostCount, arg.LikeCount, arg.FollowerCount, ) @@ -66,7 +66,7 @@ func (q *Queries) InsertProfile(ctx context.Context, arg InsertProfileParams) (P err := row.Scan( &i.UserID, &i.Bio, - &i.BlogCount, + &i.PostCount, &i.LikeCount, &i.FollowerCount, ) @@ -77,16 +77,16 @@ const updateProfile = `-- name: UpdateProfile :one UPDATE profiles SET bio = COALESCE($1,bio), - blog_count = COALESCE($2,blog_count), + post_count = COALESCE($2,post_count), like_count = COALESCE($3,like_count), follower_count = COALESCE($4,follower_count) WHERE user_id = $5 -RETURNING user_id, bio, blog_count, like_count, follower_count +RETURNING user_id, bio, post_count, like_count, follower_count ` type UpdateProfileParams struct { Bio pgtype.Text `json:"bio"` - BlogCount pgtype.Int4 `json:"blog_count"` + PostCount pgtype.Int4 `json:"post_count"` LikeCount pgtype.Int4 `json:"like_count"` FollowerCount pgtype.Int4 `json:"follower_count"` UserID int32 `json:"user_id"` @@ -95,7 +95,7 @@ type UpdateProfileParams struct { func (q *Queries) UpdateProfile(ctx context.Context, arg UpdateProfileParams) (Profile, error) { row := q.db.QueryRow(ctx, updateProfile, arg.Bio, - arg.BlogCount, + arg.PostCount, arg.LikeCount, arg.FollowerCount, arg.UserID, @@ -104,7 +104,7 @@ func (q *Queries) UpdateProfile(ctx context.Context, arg UpdateProfileParams) (P err := row.Scan( &i.UserID, &i.Bio, - &i.BlogCount, + &i.PostCount, &i.LikeCount, &i.FollowerCount, ) diff --git a/db/sqlc/profiles_test.go b/db/sqlc/profiles_test.go index 7cc5470..b128796 100644 --- a/db/sqlc/profiles_test.go +++ b/db/sqlc/profiles_test.go @@ -24,7 +24,7 @@ func createRandomUserProfile(t *testing.T) Profile { require.Equal(t, arg.UserID, profile.UserID) require.Equal(t, arg.Bio, profile.Bio) - require.Equal(t, arg.BlogCount, profile.BlogCount) + require.Equal(t, arg.PostCount, profile.PostCount) require.Equal(t, arg.FollowerCount, profile.FollowerCount) require.Equal(t, arg.LikeCount, profile.LikeCount) @@ -42,7 +42,7 @@ func TestGetProfile(t *testing.T) { require.NotEmpty(t, profile) require.Equal(t, randomProfile.UserID, profile.UserID) require.Equal(t, randomProfile.Bio, profile.Bio) - require.Equal(t, randomProfile.BlogCount, profile.BlogCount) + require.Equal(t, randomProfile.PostCount, profile.PostCount) require.Equal(t, randomProfile.FollowerCount, profile.FollowerCount) require.Equal(t, randomProfile.LikeCount, profile.LikeCount) @@ -65,16 +65,16 @@ func TestUpdateProfileBio(t *testing.T) { require.Equal(t, randomProfile.UserID, profile.UserID) require.NotEqual(t, randomProfile.Bio, profile.Bio) - require.Equal(t, randomProfile.BlogCount, profile.BlogCount) + require.Equal(t, randomProfile.PostCount, profile.PostCount) require.Equal(t, randomProfile.FollowerCount, profile.FollowerCount) require.Equal(t, randomProfile.LikeCount, profile.LikeCount) } -func TestUpdateProfileBlogCount(t *testing.T) { +func TestUpdateProfilePostCount(t *testing.T) { randomProfile := createRandomUserProfile(t) updateProfile := UpdateProfileParams{ UserID: randomProfile.UserID, - BlogCount: pgtype.Int4{ + PostCount: pgtype.Int4{ Valid: true, Int32: util.RandomLike(), }, @@ -87,7 +87,7 @@ func TestUpdateProfileBlogCount(t *testing.T) { require.Equal(t, randomProfile.UserID, profile.UserID) require.Equal(t, randomProfile.Bio, profile.Bio) - require.NotEqual(t, randomProfile.BlogCount, profile.BlogCount) + require.NotEqual(t, randomProfile.PostCount, profile.PostCount) require.Equal(t, randomProfile.FollowerCount, profile.FollowerCount) require.Equal(t, randomProfile.LikeCount, profile.LikeCount) } @@ -109,7 +109,7 @@ func TestUpdateProfileFollowerCount(t *testing.T) { require.Equal(t, randomProfile.UserID, profile.UserID) require.Equal(t, randomProfile.Bio, profile.Bio) - require.Equal(t, randomProfile.BlogCount, profile.BlogCount) + require.Equal(t, randomProfile.PostCount, profile.PostCount) require.NotEqual(t, randomProfile.FollowerCount, profile.FollowerCount) require.Equal(t, randomProfile.LikeCount, profile.LikeCount) } @@ -131,7 +131,7 @@ func TestUpdateProfileLikeCount(t *testing.T) { require.Equal(t, randomProfile.UserID, profile.UserID) require.Equal(t, randomProfile.Bio, profile.Bio) - require.Equal(t, randomProfile.BlogCount, profile.BlogCount) + require.Equal(t, randomProfile.PostCount, profile.PostCount) require.Equal(t, randomProfile.FollowerCount, profile.FollowerCount) require.NotEqual(t, randomProfile.LikeCount, profile.LikeCount) } @@ -144,7 +144,7 @@ func TestUpdateProfileAll(t *testing.T) { Valid: true, String: util.RandomDescription(), }, - BlogCount: pgtype.Int4{ + PostCount: pgtype.Int4{ Valid: true, Int32: util.RandomLike(), }, @@ -165,12 +165,12 @@ func TestUpdateProfileAll(t *testing.T) { require.Equal(t, randomProfile.UserID, profile.UserID) require.NotEqual(t, randomProfile.Bio, profile.Bio) - require.NotEqual(t, randomProfile.BlogCount, profile.BlogCount) + require.NotEqual(t, randomProfile.PostCount, profile.PostCount) require.NotEqual(t, randomProfile.FollowerCount, profile.FollowerCount) require.NotEqual(t, randomProfile.LikeCount, profile.LikeCount) require.Equal(t, updateProfile.Bio, profile.Bio) - require.Equal(t, updateProfile.BlogCount, profile.BlogCount) + require.Equal(t, updateProfile.PostCount, profile.PostCount) require.Equal(t, updateProfile.FollowerCount, profile.FollowerCount) require.Equal(t, updateProfile.LikeCount, profile.LikeCount) } diff --git a/db/sqlc/querier.go b/db/sqlc/querier.go index 7811af8..44e3824 100644 --- a/db/sqlc/querier.go +++ b/db/sqlc/querier.go @@ -11,41 +11,41 @@ import ( ) type Querier interface { - DeleteBlog(ctx context.Context, id int32) error - DeleteBlogCategory(ctx context.Context, arg DeleteBlogCategoryParams) error - DeleteBlogTag(ctx context.Context, arg DeleteBlogTagParams) error DeleteCategory(ctx context.Context, id int32) error DeleteComment(ctx context.Context, id int32) error DeleteFeaturedStory(ctx context.Context, id int32) error DeleteOnboarding(ctx context.Context, id int32) error + DeletePost(ctx context.Context, id int32) error + DeletePostCategory(ctx context.Context, arg DeletePostCategoryParams) error + DeletePostTag(ctx context.Context, arg DeletePostTagParams) error DeleteProfile(ctx context.Context, userID int32) error DeleteTag(ctx context.Context, id int32) error DeleteUser(ctx context.Context, id int32) error DeleteUserFollower(ctx context.Context, arg DeleteUserFollowerParams) error DeleteUserPost(ctx context.Context, arg DeleteUserPostParams) error - GetBlog(ctx context.Context, id int32) (Blog, error) - GetBlogs(ctx context.Context, arg GetBlogsParams) ([]Blog, error) - GetCategoriesForBlog(ctx context.Context, blogID int32) ([]Category, error) + GetCategoriesForPost(ctx context.Context, postID int32) ([]Category, error) GetCategory(ctx context.Context, id int32) (Category, error) - GetCommentsForBlog(ctx context.Context, blogID int32) ([]Comment, error) + GetCommentsForPost(ctx context.Context, postID int32) ([]Comment, error) GetFeaturedStory(ctx context.Context, id int32) (FeaturedStory, error) GetFollowersOfUser(ctx context.Context, userID int32) ([]GetFollowersOfUserRow, error) GetFollowingUsers(ctx context.Context, followerID int32) ([]GetFollowingUsersRow, error) GetOnboarding(ctx context.Context, id int32) (Onboarding, error) + GetPost(ctx context.Context, id int32) (Post, error) + GetPosts(ctx context.Context, arg GetPostsParams) ([]Post, error) GetProfile(ctx context.Context, userID int32) (Profile, error) GetSession(ctx context.Context, id uuid.UUID) (Session, error) GetTag(ctx context.Context, id int32) (Tag, error) - GetTagsForBlog(ctx context.Context, blogID int32) ([]Tag, error) + GetTagsForPost(ctx context.Context, postID int32) ([]Tag, error) GetUser(ctx context.Context, username string) (User, error) - GetUserBlog(ctx context.Context, arg GetUserBlogParams) (GetUserBlogRow, error) - GetUserBlogs(ctx context.Context, userID int32) ([]GetUserBlogsRow, error) - InsertBlog(ctx context.Context, arg InsertBlogParams) (Blog, error) - InsertBlogCategory(ctx context.Context, arg InsertBlogCategoryParams) (BlogCategory, error) - InsertBlogTag(ctx context.Context, arg InsertBlogTagParams) (BlogTag, error) + GetUserPost(ctx context.Context, arg GetUserPostParams) (GetUserPostRow, error) + GetUserPosts(ctx context.Context, userID int32) ([]GetUserPostsRow, error) InsertCategory(ctx context.Context, name string) (Category, error) InsertComment(ctx context.Context, arg InsertCommentParams) (Comment, error) InsertFeaturedStory(ctx context.Context, arg InsertFeaturedStoryParams) (FeaturedStory, error) InsertOnboarding(ctx context.Context, arg InsertOnboardingParams) (Onboarding, error) + InsertPost(ctx context.Context, arg InsertPostParams) (Post, error) + InsertPostCategory(ctx context.Context, arg InsertPostCategoryParams) (PostCategory, error) + InsertPostTag(ctx context.Context, arg InsertPostTagParams) (PostTag, error) InsertProfile(ctx context.Context, arg InsertProfileParams) (Profile, error) InsertSession(ctx context.Context, arg InsertSessionParams) (Session, error) InsertTag(ctx context.Context, name string) (Tag, error) @@ -53,10 +53,10 @@ type Querier interface { InsertUserFollower(ctx context.Context, arg InsertUserFollowerParams) (UserFollower, error) InsertUserPost(ctx context.Context, arg InsertUserPostParams) (UserPost, error) ListOnboarding(ctx context.Context) ([]Onboarding, error) - UpdateBlog(ctx context.Context, arg UpdateBlogParams) (Blog, error) UpdateCategory(ctx context.Context, arg UpdateCategoryParams) (Category, error) UpdateFeaturedStory(ctx context.Context, arg UpdateFeaturedStoryParams) (FeaturedStory, error) UpdateOnboarding(ctx context.Context, arg UpdateOnboardingParams) (Onboarding, error) + UpdatePost(ctx context.Context, arg UpdatePostParams) (Post, error) UpdateProfile(ctx context.Context, arg UpdateProfileParams) (Profile, error) UpdateTag(ctx context.Context, arg UpdateTagParams) (Tag, error) UpdateUser(ctx context.Context, arg UpdateUserParams) (User, error) diff --git a/db/sqlc/user_posts.sql.go b/db/sqlc/user_posts.sql.go index 83b0731..eafc932 100644 --- a/db/sqlc/user_posts.sql.go +++ b/db/sqlc/user_posts.sql.go @@ -13,32 +13,32 @@ import ( const deleteUserPost = `-- name: DeleteUserPost :exec DELETE FROM user_posts -WHERE user_id = $1 AND blog_id = $2 +WHERE user_id = $1 AND post_id = $2 ` type DeleteUserPostParams struct { UserID int32 `json:"user_id"` - BlogID int32 `json:"blog_id"` + PostID int32 `json:"post_id"` } func (q *Queries) DeleteUserPost(ctx context.Context, arg DeleteUserPostParams) error { - _, err := q.db.Exec(ctx, deleteUserPost, arg.UserID, arg.BlogID) + _, err := q.db.Exec(ctx, deleteUserPost, arg.UserID, arg.PostID) return err } -const getUserBlog = `-- name: GetUserBlog :one +const getUserPost = `-- name: GetUserPost :one SELECT b.id, b.title, b.summary, b.content, b.cover_image, b.created_at, b.updated_at, b.likes -FROM blogs b -JOIN user_posts up ON up.blog_id = b.id +FROM posts b +JOIN user_posts up ON up.post_id = b.id WHERE up.user_id = $1 AND b.id = $2 ` -type GetUserBlogParams struct { +type GetUserPostParams struct { UserID int32 `json:"user_id"` ID int32 `json:"id"` } -type GetUserBlogRow struct { +type GetUserPostRow struct { ID int32 `json:"id"` Title string `json:"title"` Summary string `json:"summary"` @@ -49,9 +49,9 @@ type GetUserBlogRow struct { Likes pgtype.Int4 `json:"likes"` } -func (q *Queries) GetUserBlog(ctx context.Context, arg GetUserBlogParams) (GetUserBlogRow, error) { - row := q.db.QueryRow(ctx, getUserBlog, arg.UserID, arg.ID) - var i GetUserBlogRow +func (q *Queries) GetUserPost(ctx context.Context, arg GetUserPostParams) (GetUserPostRow, error) { + row := q.db.QueryRow(ctx, getUserPost, arg.UserID, arg.ID) + var i GetUserPostRow err := row.Scan( &i.ID, &i.Title, @@ -65,14 +65,14 @@ func (q *Queries) GetUserBlog(ctx context.Context, arg GetUserBlogParams) (GetUs return i, err } -const getUserBlogs = `-- name: GetUserBlogs :many +const getUserPosts = `-- name: GetUserPosts :many SELECT b.id, b.title, b.summary, b.content, b.cover_image, b.created_at, b.updated_at, b.likes -FROM blogs b -JOIN user_posts up ON up.blog_id = b.id +FROM posts b +JOIN user_posts up ON up.post_id = b.id WHERE up.user_id = $1 ` -type GetUserBlogsRow struct { +type GetUserPostsRow struct { ID int32 `json:"id"` Title string `json:"title"` Summary string `json:"summary"` @@ -83,15 +83,15 @@ type GetUserBlogsRow struct { Likes pgtype.Int4 `json:"likes"` } -func (q *Queries) GetUserBlogs(ctx context.Context, userID int32) ([]GetUserBlogsRow, error) { - rows, err := q.db.Query(ctx, getUserBlogs, userID) +func (q *Queries) GetUserPosts(ctx context.Context, userID int32) ([]GetUserPostsRow, error) { + rows, err := q.db.Query(ctx, getUserPosts, userID) if err != nil { return nil, err } defer rows.Close() - items := []GetUserBlogsRow{} + items := []GetUserPostsRow{} for rows.Next() { - var i GetUserBlogsRow + var i GetUserPostsRow if err := rows.Scan( &i.ID, &i.Title, @@ -113,19 +113,19 @@ func (q *Queries) GetUserBlogs(ctx context.Context, userID int32) ([]GetUserBlog } const insertUserPost = `-- name: InsertUserPost :one -INSERT INTO user_posts (user_id, blog_id) +INSERT INTO user_posts (user_id, post_id) VALUES ($1, $2) -RETURNING user_id, blog_id +RETURNING user_id, post_id ` type InsertUserPostParams struct { UserID int32 `json:"user_id"` - BlogID int32 `json:"blog_id"` + PostID int32 `json:"post_id"` } func (q *Queries) InsertUserPost(ctx context.Context, arg InsertUserPostParams) (UserPost, error) { - row := q.db.QueryRow(ctx, insertUserPost, arg.UserID, arg.BlogID) + row := q.db.QueryRow(ctx, insertUserPost, arg.UserID, arg.PostID) var i UserPost - err := row.Scan(&i.UserID, &i.BlogID) + err := row.Scan(&i.UserID, &i.PostID) return i, err } diff --git a/db/sqlc/user_posts_test.go b/db/sqlc/user_posts_test.go index 4572232..ade0ed7 100644 --- a/db/sqlc/user_posts_test.go +++ b/db/sqlc/user_posts_test.go @@ -7,43 +7,43 @@ import ( "github.com/stretchr/testify/require" ) -func createRandomPost(t *testing.T) UserPost { - randomBlog := createRandomBlog(t) +func createRandomUserPost(t *testing.T) UserPost { + randomPost := createRandomPost(t) arg := InsertUserPostParams{ - UserID: randomBlog.UserID.Int32, - BlogID: randomBlog.ID, + UserID: randomPost.UserID.Int32, + PostID: randomPost.ID, } userPost, err := testStore.InsertUserPost(context.Background(), arg) require.NoError(t, err) require.NotEmpty(t, userPost) require.Equal(t, arg.UserID, userPost.UserID) - require.Equal(t, arg.BlogID, userPost.BlogID) + require.Equal(t, arg.PostID, userPost.PostID) return userPost } func TestCreateUserPost(t *testing.T) { - createRandomPost(t) + createRandomUserPost(t) } func TestGetUserPost(t *testing.T) { - randomUserPost := createRandomPost(t) - userPost, err := testStore.GetUserBlogs(context.Background(), randomUserPost.UserID) + randomUserPost := createRandomUserPost(t) + userPost, err := testStore.GetUserPosts(context.Background(), randomUserPost.UserID) require.NoError(t, err) require.NotEmpty(t, userPost) } func TestDeleteUserPost(t *testing.T) { - randomUserPost := createRandomPost(t) + randomUserPost := createRandomUserPost(t) err := testStore.DeleteUserPost(context.Background(), DeleteUserPostParams{ UserID: randomUserPost.UserID, - BlogID: randomUserPost.BlogID, + PostID: randomUserPost.PostID, }) require.NoError(t, err) - userPost, err := testStore.GetUserBlog(context.Background(), GetUserBlogParams{ + userPost, err := testStore.GetUserPost(context.Background(), GetUserPostParams{ UserID: randomUserPost.UserID, - ID: randomUserPost.BlogID, + ID: randomUserPost.PostID, }) require.Error(t, err) require.EqualError(t, err, ErrRecordNotFound.Error())