add author subscription
This commit is contained in:
parent
3828e92c19
commit
32becea4da
|
@ -33,6 +33,14 @@ class UserRole(Base):
|
|||
user_id = Column(ForeignKey('user.id'), primary_key = True)
|
||||
role_id = Column(ForeignKey('role.id'), primary_key = True)
|
||||
|
||||
class AuthorSubscription(Base):
|
||||
__tablename__ = "author_subscription"
|
||||
|
||||
id = None
|
||||
subscriber = Column(ForeignKey('user.slug'), primary_key = True)
|
||||
author = Column(ForeignKey('user.slug'), primary_key = True)
|
||||
createdAt = Column(DateTime, nullable=False, default = datetime.now, comment="Created at")
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = "user"
|
||||
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
from orm import User, UserRole, Role, UserRating
|
||||
from orm.user import AuthorSubscription
|
||||
from orm.base import local_session
|
||||
from resolvers.base import mutation, query, subscription
|
||||
from auth.authenticate import login_required
|
||||
|
||||
from sqlalchemy import func
|
||||
from sqlalchemy import func, and_
|
||||
from sqlalchemy.orm import selectinload
|
||||
import asyncio
|
||||
|
||||
|
@ -46,3 +47,31 @@ async def update_profile(_, info, profile):
|
|||
session.commit()
|
||||
|
||||
return {}
|
||||
|
||||
@mutation.field("authorSubscribe")
|
||||
@login_required
|
||||
async def author_subscribe(_, info, slug):
|
||||
user = info.context["request"].user
|
||||
|
||||
AuthorSubscription.create(
|
||||
subscriber = user.slug,
|
||||
author = slug
|
||||
)
|
||||
|
||||
return {}
|
||||
|
||||
@mutation.field("authorUnsubscribe")
|
||||
@login_required
|
||||
async def author_unsubscribe(_, info, slug):
|
||||
user = info.context["request"].user
|
||||
|
||||
with local_session() as session:
|
||||
sub = session.query(AuthorSubscription).\
|
||||
filter(and_(AuthorSubscription.subscriber == user.slug, AuthorSubscription.author == slug)).\
|
||||
first()
|
||||
if not sub:
|
||||
return { "error" : "subscription not exist" }
|
||||
session.delete(sub)
|
||||
session.commit()
|
||||
|
||||
return {}
|
||||
|
|
|
@ -150,6 +150,9 @@ type Mutation {
|
|||
createCommunity(title: String!, desc: String!): Community!
|
||||
updateCommunity(community: CommunityInput!): Community!
|
||||
deleteCommunity(id: Int!): Result!
|
||||
|
||||
authorSubscribe(slug: String!): Result!
|
||||
authorUnsubscribe(slug: String!): Result!
|
||||
}
|
||||
|
||||
################################### Query
|
||||
|
|
Loading…
Reference in New Issue
Block a user