Skip to content

Commit

Permalink
Merge pull request OfficeDev#15 from OfficeDev/zhaofeng/share-now-update
Browse files Browse the repository at this point in the history
update share-now-api
  • Loading branch information
xzf0587 authored May 12, 2021
2 parents 2543718 + 450f945 commit 99b7adf
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 25 deletions.
2 changes: 1 addition & 1 deletion share-now/.fx/manifest.source.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"full": "shareNow"
},
"description": {
"short": "2Encourage co-workers to knowledge-based sharing of posts, videos, or topics.",
"short": "Encourage co-workers to knowledge-based sharing of posts, videos, or topics.",
"full": "Encourage and introduce your co-workers to a widespread knowledge-based sharing of posts, videos, or topics with a bite-sized information on how it influenced them and learn together in Teams."
},
"accentColor": "#FFFFFF",
Expand Down
4 changes: 3 additions & 1 deletion share-now/api/posts/function.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"name": "req",
"methods": [
"get",
"post"
"post",
"delete",
"put"
],
"route": "posts/{id:int?}"
},
Expand Down
49 changes: 44 additions & 5 deletions share-now/api/posts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import {
loadConfiguration,
OnBehalfOfUserCredential,
} from "teamsdev-client";
import { executeQuery, getSQLConnection, ResponsePost } from "../utils/common";
import { executeQuery, getSQLConnection, PostRequest, PostTypes, ResponsePost } from "../utils/common";
import { checkPost } from "../utils/query";

interface Response {
status: number;
body: { [key: string]: any; };
body: any;
}

type TeamsfxContext = { [key: string]: any; };
Expand All @@ -37,6 +38,8 @@ export default async function run(
const credential = new OnBehalfOfUserCredential(accessToken);
const currentUser = await credential.getUserInfo();
let query;
let postID;
let check;

switch (method) {
case "get":
Expand All @@ -45,12 +48,34 @@ export default async function run(
query = `SELECT * FROM [dbo].[TeamPostEntity] where IsRemoved = 0 ORDER BY PostID DESC OFFSET ${pageSize * pageCount} ROWS FETCH NEXT ${pageSize} ROWS ONLY;`;
const posts = await executeQuery(query, connection);
const data = await decoratePosts(posts, currentUser.objectId, connection);
res.body["data"] = data;
res.body = data;
return res;
case "post":
query = `INSERT TeamPostEntity (ContentUrl, CreatedByName, CreatedDate, Description, IsRemoved, Tags, Title, TotalVotes, Type, UpdatedDate, UserID) VALUES ('https://bing.com','zhaofeng xu', CURRENT_TIMESTAMP, 'hello', 0, 'red', 'manual post', 0,1, CURRENT_TIMESTAMP, '${currentUser.objectId}');`;
const createRequest = getPostRequest(req);
query = `INSERT TeamPostEntity (ContentUrl, CreatedByName, CreatedDate, Description, IsRemoved, Tags, Title, TotalVotes, Type, UpdatedDate, UserID) VALUES ('${createRequest.contentUrl}','${currentUser.displayName}', CURRENT_TIMESTAMP, '${createRequest.description}', 0, '${createRequest.tags}', '${createRequest.title}', 0,${createRequest.type}, CURRENT_TIMESTAMP, '${currentUser.objectId}');`;
await executeQuery(query, connection);
res.body["data"] = "create post successfully";
res.body = "create post successfully";
return res;
case "delete":
postID = context.bindingData.id as number;
check = await checkPost(postID, connection, currentUser.objectId);
if (!check) {
throw new Error("invalid postID");
}
query = `update TeamPostEntity set IsRemoved = 1 where PostID = ${postID}`;
await executeQuery(query, connection);
res.body = "delete post successfully";
return res;
case "put":
postID = context.bindingData.id as number;
check = await checkPost(postID, connection, currentUser.objectId);
if (!check) {
throw new Error("invalid postID");
}
const updateRequest = getPostRequest(req);
query = `update TeamPostEntity set ContentUrl = '${updateRequest.contentUrl}', Description = '${updateRequest.description}', Tags = '${updateRequest.tags}', Title = '${updateRequest.title}', Type = ${updateRequest.type}, UpdatedDate = CURRENT_TIMESTAMP where PostID = ${postID};`;
await executeQuery(query, connection);
res.body = "update post successfully";
return res;
}
} catch (error) {
Expand Down Expand Up @@ -80,4 +105,18 @@ async function decoratePosts(posts, userID, connection) {
return element;
});
return elements;
}

function getPostRequest(req: HttpRequest) {
let res = new PostRequest();
res.type = req.body.type ?? 1;
res.title = req.body.title ?? "automatic post";
res.description = req.body.description ?? "hello";
res.contentUrl = req.body.contentUrl ?? "https://bing.com";
res.tags = req.body.tags ?? "";

if (!Object.values(PostTypes).includes(res.type)) {
throw new Error("invalid input for type");
}
return res;
}
18 changes: 17 additions & 1 deletion share-now/api/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class ResponsePost {
this.title = post.Title;
this.description = post.Description
this.contentUrl = post.ContentUrl
this.tags = post.tags
this.tags = post.Tags
this.createdDate = post.CreatedDate
this.createdByName = post.CreatedByName
this.userId = post.UserID
Expand All @@ -79,3 +79,19 @@ export class ResponsePost {
this.isRemoved = post.IsRemoved
}
}

export class PostRequest {
type: number;
title: string;
description: string;
contentUrl: string;
tags: string;
}

export enum PostTypes {
"Article / blog" = 1,
Other = 2,
Podcast = 3,
Video = 4,
Book = 5,
}
20 changes: 11 additions & 9 deletions share-now/api/utils/query.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { executeQuery } from "./common";

export async function checkPost(postID, conn): Promise<boolean> {
let query = `select count(*) as count from [dbo].[TeamPostEntity] where PostID = ${postID} and isRemoved = 0;`;
var result = await executeQuery(query, conn);
if (result.length === 0 || result[0].count === 0) {
return false;
} else {
return true;
}
export async function checkPost(postID, conn, userID = ""): Promise<boolean> {
let userFilter = userID ? `and UserID = '${userID}'` : "";
let query = `select count(*) as count from [dbo].[TeamPostEntity] where PostID = ${postID} and isRemoved = 0 ${userFilter};`;
var result = await executeQuery(query, conn);
if (result.length === 0 || result[0].count === 0) {
return false;
} else {
return true;
}
}
8 changes: 4 additions & 4 deletions share-now/api/vote/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { checkPost } from "../utils/query";

interface Response {
status: number;
body: { [key: string]: any; };
body: any;
}

type TeamsfxContext = { [key: string]: any; };
Expand Down Expand Up @@ -48,18 +48,18 @@ export default async function run(
if (method === "post") {
const voted = await checkVoted(postID, currentUser.objectId, connection);
if (voted) {
res.body["data"] = "already voted";
res.body = "already voted";
return res;
}
query = `INSERT [dbo].[UserVoteEntity] (PostID, UserID) VALUES (${postID},'${currentUser.objectId}');`;
await executeQuery(query, connection);
await updateVoteCount(postID, true, connection)
res.body["data"] = "vote successfully";
res.body = "vote successfully";
return res;
} else if (method === "delete") {
const voted = await checkVoted(postID, currentUser.objectId, connection);
if (!voted) {
res.body["data"] = "haven't voted";
res.body = "haven't voted";
return res;
}
query = `delete [dbo].[UserVoteEntity] where UserID = '${currentUser.objectId}' and PostID = ${postID};`;
Expand Down
8 changes: 4 additions & 4 deletions share-now/bot/messageExtensionBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ export class MessageExtensionBot extends TeamsActivityHandler {
let connection;
try {
connection = await getSQLConnection();
const searchQuery = query.parameters[0].value;
const searchItem = query.parameters[0].value;
let sqlQuery: string;
if (query.commandId === "allItems") {
sqlQuery = buildQuery(searchQuery, query.queryOptions.skip, query.queryOptions.count);
sqlQuery = buildQuery(searchItem, query.queryOptions.skip, query.queryOptions.count);
} else {
sqlQuery = buildQuery(searchQuery, query.queryOptions.skip, query.queryOptions.count, context.activity.from.aadObjectId);
sqlQuery = buildQuery(searchItem, query.queryOptions.skip, query.queryOptions.count, context.activity.from.aadObjectId);
}
var result = await executeQuery(sqlQuery, connection);
const attachments = [];
Expand Down Expand Up @@ -175,7 +175,7 @@ function voteIcon() {

function buildQuery(search, skip, count, userID = "") {
let userFilter = userID ? `and UserID = '${userID}'` : "";
let sqlQuery = `SELECT * FROM [dbo].[TeamPostEntity] where Title like '%${search}%' ${userFilter} ORDER BY PostID DESC OFFSET ${skip} ROWS FETCH NEXT ${count} ROWS ONLY;`;
let sqlQuery = `SELECT * FROM [dbo].[TeamPostEntity] where IsRemoved = 0 and (Title like '%${search}%' or Tags like '%${search}%') ${userFilter} ORDER BY PostID DESC OFFSET ${skip} ROWS FETCH NEXT ${count} ROWS ONLY;`;
return sqlQuery;
}

Expand Down

0 comments on commit 99b7adf

Please sign in to comment.