post.service.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { PrismaService } from '@/prisma/prisma.service'
  2. import { Injectable } from '@nestjs/common'
  3. @Injectable()
  4. export class PostService {
  5. constructor(private readonly prisma: PrismaService) {}
  6. async post(authId: number, plateid: number, { title, content }) {
  7. return await this.prisma.post.create({
  8. data: {
  9. title,
  10. content,
  11. authorId: authId,
  12. plateId: plateid,
  13. },
  14. })
  15. }
  16. async updated(postid: number, { title, content, plateId }) {
  17. if (((await this.getpost(postid)) as any).cod !== 400) {
  18. return await this.prisma.post.update({
  19. where: {
  20. id: postid,
  21. },
  22. data: {
  23. title,
  24. content,
  25. plateId,
  26. },
  27. })
  28. }
  29. return { cod: 400, message: '帖子不存在' }
  30. }
  31. async delete(postid: number) {
  32. if (((await this.getpost(postid)) as any).cod !== 400) {
  33. return await this.prisma.post.delete({
  34. where: {
  35. id: postid,
  36. },
  37. })
  38. }
  39. return { cod: 400, message: '帖子不存在' }
  40. }
  41. async getpost(postid: number) {
  42. const data = await this.prisma.post.findUnique({
  43. where: {
  44. id: postid,
  45. },
  46. include: {
  47. author: true,
  48. plate: true,
  49. comment: true,
  50. },
  51. })
  52. if (!data) {
  53. return { cod: 400, message: '帖子不存在' }
  54. }
  55. delete data.author.password
  56. delete data.author.username
  57. delete data.author.email
  58. delete data.plate.id
  59. delete data.id
  60. delete data.authorId
  61. delete data.plateId
  62. return data
  63. }
  64. async getpostlist(plateid: number) {
  65. const data = await this.prisma.post.findMany({
  66. where: {
  67. plateId: plateid,
  68. },
  69. select: {
  70. id: true,
  71. title: true,
  72. authorId: true,
  73. },
  74. })
  75. data.map(async (item) => {
  76. const user = await this.prisma.auth.findUnique({
  77. where: {
  78. auth_id: item.authorId,
  79. },
  80. include: {
  81. user: true,
  82. },
  83. })
  84. delete user.password
  85. delete user.user.authId
  86. return {
  87. ...item,
  88. ...user,
  89. }
  90. })
  91. return data
  92. }
  93. }