forked from hexlet-rus/runit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[hexlet-rus#537] Implement admin section
- The migration file has been added, the isAdmin column has been added to the users table. - The migration file implements the logic of installing the admin for the email specified in the environment variable ADMIN_EMAIL. - The admin module has been added. - Added a controller to get a user template. - In the user creation method, the logic of installing the admin during registration has been added. На данном этапе: - Добавлен файл миграции, добавлена колонка isAdmin в таблицу users. - В файле миграции реализована логика установки админа для емейла указанного в переменной окружения ADMIN_EMAIL. - Добавлен admin module. - Добавлен контроллер для получения шаблона пользователей. - В методе создания пользователя добавлена логика установки админа при регистрации.
- Loading branch information
1 parent
b33fef0
commit 85e73d4
Showing
13 changed files
with
1,276 additions
and
692 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,6 @@ GITHUB_USER_URL=https://api.github.com/user | |
SENTRY_DSN=https://[email protected]/000000 | ||
FRONTEND_URL=http://localhost:3000 | ||
EMAIL_FROM=runit@localhost | ||
|
||
|
||
ADMIN_EMAIL=[email protected] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* eslint-disable no-useless-constructor */ | ||
|
||
import { | ||
Controller, | ||
DefaultValuePipe, | ||
Get, | ||
ParseIntPipe, | ||
Query, | ||
Render, | ||
} from '@nestjs/common'; | ||
import { User } from '../entities/user.entity'; | ||
import { AdminsService } from './admins.service'; | ||
|
||
@Controller('admin') | ||
export class AdminsController { | ||
constructor(private readonly adminsService: AdminsService) {} | ||
|
||
@Get('users') | ||
@Render('users.pug') | ||
async findAllUsers( | ||
@Query('page', new DefaultValuePipe(1), ParseIntPipe) page = 1, | ||
) { | ||
const take = 10; | ||
const users: User[] = await this.adminsService.findAllUsers(page, take); | ||
return { users, currentPage: page }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { UsersModule } from '../users/users.module'; | ||
import { AdminsController } from './admins.controller'; | ||
import { AdminsService } from './admins.service'; | ||
|
||
@Module({ | ||
imports: [UsersModule], | ||
providers: [AdminsService], | ||
controllers: [AdminsController], | ||
}) | ||
export class AdminsModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* eslint-disable no-useless-constructor */ | ||
|
||
import { Injectable } from '@nestjs/common'; | ||
import { User } from '../entities/user.entity'; | ||
import { UsersService } from '../users/users.service'; | ||
|
||
@Injectable() | ||
export class AdminsService { | ||
constructor(private readonly usersService: UsersService) {} | ||
|
||
findAllUsers(page: number, take: number): Promise<User[]> { | ||
return this.usersService.findAllUsers(page, take); | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
doctype html | ||
html | ||
head | ||
meta(charset='utf-8') | ||
meta(name='viewport' content='width=device-width, initial-scale=1') | ||
title User List | ||
link(href=' https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css' rel='stylesheet') | ||
body | ||
//- script(src='https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js') | ||
nav.navbar.navbar-expand-lg.bg-body-tertiary | ||
.container-fluid | ||
.collapse.navbar-collapse#navbarSupportedContent | ||
ul.navbar-nav.me-auto.mb-2.mb-lg-0 | ||
li.nav-item | ||
a.nav-link(href='#') На главную | ||
li.nav-item | ||
a.nav-link(href='#') Пользователи | ||
li.nav-item | ||
a.nav-link(href='#') Сниппеты | ||
.container | ||
.d-flex.align-items-center.justify-content-center | ||
h1.text-center.font-weight-bold.pb-3 | ||
| Пользователи | ||
table.table | ||
thead | ||
tr | ||
th ID | ||
th Имя | ||
th Email | ||
th Админ | ||
th Действия | ||
tbody | ||
each user in users | ||
tr | ||
td= user.id | ||
td= user.username | ||
td= user.email | ||
td= user.isAdmin ? 'Да' : 'Нет' | ||
td | ||
div | ||
a.link-dark.link-offset-2.link-underline-opacity-25.link-underline-opacity-100-hover(href='#') Редактировать | ||
div | ||
a.link-dark.link-offset-2.link-underline-opacity-25.link-underline-opacity-100-hover(href='#') Удалить | ||
if currentPage > 1 | ||
a(href=`users?page=${currentPage - 1}`) Previos | ||
if users.length >= 10 | ||
a(href=`users?page=${currentPage + 1}`) Next |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
backend/src/migrations/1736530329399-add_isAdmin_column_table_users.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { MigrationInterface, QueryRunner, TableColumn } from 'typeorm'; | ||
import { config } from 'dotenv'; | ||
|
||
config(); | ||
export class AddIsAdminColumnTableUsers1736530329399 | ||
implements MigrationInterface | ||
{ | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.addColumn( | ||
'users', | ||
new TableColumn({ | ||
name: 'isAdmin', | ||
type: 'boolean', | ||
default: false, | ||
}), | ||
); | ||
|
||
const adminEmail = process.env.ADMIN_EMAIL; | ||
|
||
await queryRunner.query( | ||
`UPDATE users SET isAdmin = true WHERE email = $1`, | ||
[adminEmail], | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.dropColumn('users', 'isAdmin'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.