Skip to content

Commit

Permalink
add tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
dzencot committed Sep 19, 2024
1 parent 31e0b07 commit db1fe16
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 3 deletions.
27 changes: 27 additions & 0 deletions __fixtures__/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[
{
"id": "c7c2fa01-3174-4cc4-9a80-092b47ade67d",
"title": "Опубликовать курс по основам JavaScript",
"description": "Автор подготовил курс по JavaScript. Нужно его опубликовать"
},
{
"id": "a0b2bbd9-db89-434d-99bd-cbc04477947f",
"title": "Опубликовать курс по основам Ruby",
"description": "Автор подготовил курс по Ruby. Нужно его опубликовать"
},
{
"id": "2099a564-4819-499f-a726-97f37613f78d",
"title": "Опубликовать курс по основам PHP",
"description": "Автор подготовил курс по PHP. Нужно его опубликовать"
},
{
"id": "6178c6ad-998e-484e-a8d2-cfe10978ea17",
"title": "Опубликовать курс по основам Java",
"description": "Автор подготовил курс по Java. Нужно его опубликовать"
},
{
"id": "2149669f-517c-41a6-8d09-8683fec99eec",
"title": "Опубликовать курс по основам Python",
"description": "Автор подготовил курс по Python. Нужно его опубликовать"
}
]
Empty file added openapitools.json
Empty file.
44 changes: 44 additions & 0 deletions server/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,50 @@ const app = async (host, port) => {
return res.status(204).send();
},

// Tasks handlers
TaskService_create: (c, req, res) => {
const {
title,
description,
} = c.request.body;
const task = {
id: getId(),
title,
description
};

state.tasks.push(task);
return res.status(200).send(task);
},
TaskService_list: (c, req, res) => prepareListData('tasks', state, c),
TaskService_get: (c, req, res) => {
const { id } = c.request.params;
const { select } = c.request.query.select ?? {};
const task = state.tasks.find((item) => item.id === id);
if (!task) {
return res.status(404).send({ code: 404, message: 'Not found' });
}
return res.status(200).send(prepareItem(task, select));
},
TaskService_update: (c, req, res) => {
const { id } = c.request.params;
const index = state.tasks.findIndex((item) => item.id === id);
if (index === -1) {
return res.status(404).send({ code: 404, message: 'Not found' });
}
state.tasks[index] = {
...state.tasks[index],
...c.request.body,
};
return res.status(200).send(state.tasks[index]);
},
TaskService_delete: (c, req, res) => {
const { id } = c.request.params;
const tasks = state.tasks.filter((item) => item.id !== id);
state.tasks = tasks;
return res.status(204).send();
},

validationFail: (c, _req, res) => res.status(400).send({ code: 400, message: c.validation.errors }),
unauthorizedHandler: (c, req, res) => res
.status(401)
Expand Down
2 changes: 2 additions & 0 deletions server/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import users from '../__fixtures__/users.json' with { type: 'json' };
import posts from '../__fixtures__/posts.json' with { type: 'json' };
import comments from '../__fixtures__/comments.json' with { type: 'json' };
import courses from '../__fixtures__/courses.json' with { type: 'json' };
import tasks from '../__fixtures__/tasks.json' with { type: 'json' };
import tokens from '../__fixtures__/tokens.json' with { type: 'json' };
import appConfig from '../app.config.json' with { type: 'json' };

Expand All @@ -17,6 +18,7 @@ export const getInitData = () => ({
comments,
tokens,
courses,
tasks,
appConfig,
});

Expand Down
1 change: 1 addition & 0 deletions typescpec/main.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "./services/postsService.tsp";
import "./services/commentsService.tsp";
import "./services/authService.tsp";
import "./services/coursesService.tsp";
import "./services/tasksService.tsp";

using TypeSpec.Versioning;

Expand Down
57 changes: 57 additions & 0 deletions typescpec/models/task.tsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
@example(
#{
title: "New Task",
description: "description"
},
#{ title: "New Task" }
)
model NewTaskDto {
@minLength(1)
title: string;

@minLength(1)
description: string;

}

@example(
#{
title: "Edited Task",
description: "new description",
},
#{ title: "Edit Task" }
)
model EditTaskDto {
@minLength(1)
title?: string;

@minLength(1)
description?: string;
}

@example(
#{
id: "some-id",
title: "Edited Task",
description: "new description"
},
#{ title: "Task" }
)
model Task {
@key
id: string;

@minLength(1)
title: string;

@minLength(1)
description: string;

}

model Tasks {
tasks: Task[];
total: integer;
skip: integer = 0;
limit: integer = 30;
}
3 changes: 0 additions & 3 deletions typescpec/services/coursesService.tsp
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import "@typespec/http";
import "@typespec/rest";
import "../models/course.tsp";
import "../models/post.tsp";
import "../models/comment.tsp";
import "../models/error.tsp";


using TypeSpec.Http;
Expand Down
44 changes: 44 additions & 0 deletions typescpec/services/tasksService.tsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import "@typespec/http";
import "@typespec/rest";
import "../models/task.tsp";


using TypeSpec.Http;
using TypeSpec.Rest;

namespace AppService;

@tag("Tasks")
@route("/tasks")
interface TaskService {
@get
op list(
@query skip?: string,
@query limit?: string,
@query select?: string[]
): Tasks;

@get
@opExample(
#{ parameters: #{ id: "some-id" } }
)
op get(
@path id: string,
@query select?: string
): Task;

@post
op create(...NewTaskDto): Task;

@patch
@opExample(
#{ parameters: #{ id: "some-id" } }
)
op update(@path id: string, ...EditTaskDto): Task;

@delete
@opExample(
#{ parameters: #{ id: "some-id" } }
)
op delete(@path id: string): void;
}

0 comments on commit db1fe16

Please sign in to comment.