Zeichen's app for both server and client.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import FolderModel from '../../models/Folder'
  2. import * as ColumnTypes from '../../utilities/ColumnTypes'
  3. import * as Response from '../../utilities/Response'
  4. type Folder = ColumnTypes.InferModel<typeof FolderModel>
  5. export const getSingle = repository => async (id: string) => {
  6. const instance = await repository.findByPk(id)
  7. if (instance === null) {
  8. throw new Response.NotFound({ message: 'Not found.' })
  9. }
  10. return new Response.Retrieved({
  11. data: instance.toJSON() as Folder,
  12. })
  13. }
  14. export const getMultiple = repository => async (query?: Record<string, unknown>) => {
  15. const fetchMethod = async query => {
  16. if (query) {
  17. return repository.findWhere({
  18. attributes: query,
  19. })
  20. }
  21. return repository.findAll()
  22. }
  23. const instances = await fetchMethod(query)
  24. return new Response.Retrieved<Folder[]>({
  25. data: instances.map(i => i.toJSON()),
  26. })
  27. }
  28. export const save = repository => (body: Partial<Folder>) => async (id: string, idColumnName = 'id') => {
  29. const [newInstance, created] = await repository.findOrCreate({
  30. where: { [idColumnName]: id },
  31. defaults: {
  32. ...body,
  33. [idColumnName]: id,
  34. },
  35. })
  36. if (created) {
  37. return new Response.Created({
  38. data: newInstance.toJSON() as Folder
  39. })
  40. }
  41. Object.entries(body).forEach(([key, value]) => {
  42. newInstance[key] = value
  43. })
  44. newInstance[idColumnName] = id
  45. const updatedInstance = await newInstance.save()
  46. return new Response.Saved({
  47. data: updatedInstance.toJSON() as Folder
  48. })
  49. }
  50. export const remove = repository => async (id: string) => {
  51. const instanceDAO = repository.findByPk(id)
  52. if (instanceDAO === null) {
  53. throw new Response.NotFound({ message: 'Not found.' })
  54. }
  55. await instanceDAO.destroy()
  56. return new Response.Destroyed()
  57. }