add createTopic and updateTopic; fix create/update Shout

This commit is contained in:
knst-kotov 2021-12-12 18:29:51 +03:00
parent 04f2ceb629
commit 1c0e977a60
4 changed files with 53 additions and 11 deletions

View File

@ -22,7 +22,7 @@ class Topic(Base):
title: str = Column(String, nullable=False, comment="Title")
body: str = Column(String, nullable=True, comment="Body")
pic: str = Column(String, nullable=True, comment="Picture")
children = Column(JSONType, nullable=True, comment="list of children topics")
children = Column(JSONType, nullable=True, default = [], comment="list of children topics")
community = Column(ForeignKey("community.slug"), nullable=False, comment="Community")
class TopicStorage:

View File

@ -25,6 +25,33 @@ async def topics_by_author(_, info, author):
slugs.update([topic.slug for topic in shout.topics])
return await TopicStorage.get_topics(slugs)
@mutation.field("createTopic")
@login_required
async def create_topic(_, info, input):
new_topic = Topic.create(**input)
await TopicStorage.add_topic(new_topic)
return { "topic" : new_topic }
@mutation.field("updateTopic")
@login_required
async def update_topic(_, info, input):
slug = input["slug"]
session = local_session()
topic = session.query(Topic).filter(Topic.slug == slug).first()
if not topic:
return { "error" : "topic not found" }
topic.update(input)
session.commit()
session.close()
await TopicStorage.add_topic(topic)
return { "topic" : topic }
@mutation.field("topicSubscribe")
@login_required
async def topic_subscribe(_, info, slug):
@ -54,7 +81,7 @@ async def new_shout_generator(obj, info, user_id):
await ShoutSubscriptions.register_subscription(shouts_queue)
while True:
shout = await shouts_queue.get()
if topics.intersection(set(shout.topic_ids)):
if topics.intersection(set(shout.topic_slugs)):
yield shout
finally:
await ShoutSubscriptions.del_subscription(shouts_queue)

View File

@ -249,19 +249,19 @@ async def create_shout(_, info, input):
with local_session() as session:
user = session.query(User).filter(User.id == user_id).first()
topic_ids = input.get("topic_ids")
del input["topic_ids"]
topic_slugs = input.get("topic_slugs")
del input["topic_slugs"]
new_shout = Shout.create(**input)
ShoutAuthor.create(
shout = new_shout.id,
user = user_id)
for id in topic_ids:
for slug in topic_slugs:
topic = ShoutTopic.create(
shout = new_shout.id,
topic = id)
new_shout.topic_ids = topic_ids
topic = slug)
new_shout.topic_slugs = topic_slugs
task = GitTask(
input,
@ -305,7 +305,7 @@ async def update_shout(_, info, id, input):
session.commit()
session.close()
for topic in input.get("topics"):
for topic in input.get("topic_slugs"):
ShoutTopic.create(
shout = shout.id,
topic = topic)

View File

@ -27,7 +27,7 @@ input ShoutInput {
body: String!
# replyTo: String # another shout
# tags: [String] # actual values
topic_ids: [Int]
topic_slugs: [String]
title: String
subtitle: String
versionOf: String
@ -74,6 +74,20 @@ type ChatRoomResult {
room: ChatRoom!
}
input TopicInput {
slug: String!
title: String
body: String
pic: String
children: [String]
community: String!
}
type TopicResult {
error: String
topic: Topic
}
################################### Mutation
type Mutation {
@ -106,6 +120,8 @@ type Mutation {
updateProfile(profile: ProfileInput!): Result!
# topics
createTopic(input: TopicInput!): TopicResult!
updateTopic(input: TopicInput!): TopicResult!
topicSubscribe(slug: String!): Result!
topicUnsubscribe(slug: String!): Result!
@ -324,8 +340,7 @@ type Topic {
pic: String
parents: [String] # NOTE: topic can have parent topics
children: [String] # and children
cat_id: String
community: String
community: String!
}
# TODO: resolvers to add/remove topics from publication