Skip to content

Commit

Permalink
feat(floors): content-range, delete endpoint and admin page
Browse files Browse the repository at this point in the history
  • Loading branch information
Ealenn committed Jun 24, 2022
1 parent 44e5a3a commit 942b897
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 34 deletions.
14 changes: 11 additions & 3 deletions admin/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import { BASE_URL } from "./configs/BaseUrl";

import { OpenGardenAdminLayout } from './themes/Layout';

import { IoFlower, IoFlowerOutline } from "react-icons/io5";
import { TbSeeding, TbTree, TbPlant2 } from 'react-icons/tb';

import { PlantCreate, PlantEdit, PlantShow, PlantsList } from "./views/plants";
import { VarietyCreate, VarietyEdit, VarietyShow, VarietiesList } from "./views/varieties";
import { FloorCreate, FloorEdit, FloorShow, FloorsList } from "./views/floors";

const App = () => (
<Admin
Expand All @@ -25,13 +27,19 @@ const App = () => (
edit={permissions.includes('ADMIN') ? PlantEdit : null}
list={PlantsList}
show={PlantShow}
icon={IoFlower} />
icon={TbTree} />
<Resource name="varieties"
create={permissions.includes('ADMIN') ? VarietyCreate : null}
edit={permissions.includes('ADMIN') ? VarietyEdit : null}
list={VarietiesList}
show={VarietyShow}
icon={IoFlowerOutline} />
icon={TbSeeding} />
<Resource name="floors"
create={permissions.includes('ADMIN') ? FloorCreate : null}
edit={permissions.includes('ADMIN') ? FloorEdit : null}
list={FloorsList}
show={FloorShow}
icon={TbPlant2} />
</>
)}
</Admin>
Expand Down
15 changes: 3 additions & 12 deletions admin/src/themes/Menu.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { DashboardMenuItem, Menu, MenuItemLink, useResourceDefinitions, useSidebarState } from 'react-admin';
import GitHubIcon from '@mui/icons-material/GitHub';
import { TbBrandGithub, TbHome2 } from 'react-icons/tb';
import DefaultIcon from '@mui/icons-material/PlayArrow';
import { Capitalize } from '../helpers/Capitalize';
import Divider from '@mui/material/Divider';
Expand All @@ -9,18 +9,9 @@ export const OpenGardenAdminMenu = (props) => {
const resources = useResourceDefinitions()
const [open] = useSidebarState();

const menuHeaderStyle = {
'line-height': '3',
'text-align': 'center'
}
return (
<Menu {...props}>
{
open && <span style={menuHeaderStyle}>
🏡 OpenGarden {process.env.APP_VERSION || "0.0.0"}
</span>
}
<DashboardMenuItem />
<DashboardMenuItem leftIcon={<TbHome2 />} />
{Object.keys(resources).map(name => (
<MenuItemLink
key={name}
Expand All @@ -39,7 +30,7 @@ export const OpenGardenAdminMenu = (props) => {
/>
))}
<Divider />
<MenuItemLink onClick={() => { window.location.replace("https://github.com/Ealenn/OpenGarden") }} to="/github" primaryText="GitHub" leftIcon={<GitHubIcon />} />
<MenuItemLink onClick={() => { window.location.replace("https://github.com/Ealenn/OpenGarden") }} to="/github" primaryText="GitHub" leftIcon={<TbBrandGithub />} />
</Menu>
);
};
82 changes: 82 additions & 0 deletions admin/src/views/floors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import * as React from "react";
import {
List,
Datagrid,
TextField,
DateField,
Show,
RichTextField,
ReferenceOneField,
ShowButton,
EditButton,
Create,
TextInput,
SingleFieldList,
ChipField,
ArrayField,
Edit,
TabbedShowLayout,
Tab
} from "react-admin";
import { RichTextInput } from 'ra-input-rich-text';
import { StringToLabelObject } from '../helpers/StringToLabelObject';
import { GetPermissions } from "../helpers/GetPermissions";

export const FloorCreate = (props) => (
<Create {...props}>
<TextInput source="name" />
<RichTextInput source="description" multiline fullWidth />
</Create>
);

export const FloorEdit = (props) => (
<Edit {...props}>
<TextInput source="name" />
<RichTextInput source="description" multiline fullWidth />
</Edit>
);

export const FloorsList = (props) => {
const permissions = GetPermissions();
return (
<List {...props} exporter={false}>
<Datagrid isRowSelectable={() => permissions.includes('ADMIN')}>
<TextField source="name" />
<ReferenceOneField reference="profiles" source="createdBy">
<TextField source="username" />
</ReferenceOneField>
<DateField source="createdAt" />
<DateField source="updatedAt" />
<ChipField source="status" />
{permissions.includes('ADMIN') && <EditButton />}
<ShowButton />
</Datagrid>
</List>
);
};

export const FloorShow = (props) => (
<Show {...props}>
<TabbedShowLayout>
<Tab label="Summary">
<TextField source="name" />
<RichTextField source="description" />
</Tab>
<Tab label="Metadata">
<ChipField source="status" />
<ReferenceOneField reference="profiles" source="createdBy">
<TextField source="username" />
<ArrayField source="roles">
<SingleFieldList>
<StringToLabelObject>
<ChipField source="label" />
</StringToLabelObject>
</SingleFieldList>
</ArrayField>
</ReferenceOneField>
<DateField source="createdAt" />
<DateField source="updatedAt" />
</Tab>
</TabbedShowLayout>
</Show>
);
41 changes: 24 additions & 17 deletions core/src/controllers/floors/floors.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Post,
Query,
Response,
Delete,
} from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { Mapper } from '@automapper/core';
Expand All @@ -22,6 +23,7 @@ import { ErrorsRequestBody } from '../models/errors.response.body';
import { Response as Res } from 'express';
import { Roles } from '../../auth/roles/roles.decorator';
import { Role } from '../../auth/roles/role.enum';
import { PublishedState } from '../../entities/base.published.entity';

@ApiBearerAuth()
@ApiTags('Floors')
Expand All @@ -35,15 +37,12 @@ export class FloorsController {
@Roles(Role.ADMIN)
@ApiResponse({ status: 403, description: 'Forbidden' })
@ApiResponse({ status: 201, type: FloorResponseBody })
@ApiResponse({
status: 400,
description: 'Bad Request',
type: ErrorsRequestBody,
})
@ApiResponse({ status: 400, description: 'Bad Request', type: ErrorsRequestBody })
async createFloor(@Request() req, @Body() createFloorRequestBody: CreateFloorRequestBody) {
const createFloor: Floor = {
...createFloorRequestBody,
_id: null,
status: PublishedState.ONLINE,
createdAt: new Date(),
updatedAt: new Date(),
createdBy: req.user._id,
Expand All @@ -52,19 +51,31 @@ export class FloorsController {
return this.mapper.map(floor, Floor, FloorResponseBody);
}

@Delete(':floorId')
@Roles(Role.ADMIN)
@ApiResponse({ status: 403, description: 'Forbidden', type: ErrorsRequestBody })
@ApiResponse({ status: 404, description: 'Not Found', type: ErrorsRequestBody })
@ApiResponse({ status: 200, type: FloorResponseBody })
async deletePlant(@Response() res: Res, @Request() req, @Param('floorId') plantId: string) {
const floor = await this.floorsService.deleteFloor(plantId);
if (!floor) {
throw new NotFoundException();
}

const body = this.mapper.map(floor, Floor, FloorResponseBody);
return res.set({ 'Content-Range': `elements 0-1/1` }).json(body);
}

@Get(':floorId')
@ApiResponse({ status: 200, type: FloorResponseBody })
@ApiResponse({
status: 404,
description: 'Not Found',
type: ErrorsRequestBody,
})
async getFloorById(@Param('floorId') floorId: string) {
@ApiResponse({ status: 404, description: 'Not Found', type: ErrorsRequestBody })
async getFloorById(@Response() res: Res, @Param('floorId') floorId: string) {
const floor = await this.floorsService.findOneById(floorId);
if (!floor) {
throw new NotFoundException();
}
return this.mapper.map(floor, Floor, FloorResponseBody);
const body = this.mapper.map(floor, Floor, FloorResponseBody);
return res.set({ 'Content-Range': `elements 0-1/1` }).json(body);
}

@Get()
Expand All @@ -81,10 +92,6 @@ export class FloorsController {
const body: FloorSearchResponseBody = {
floors: this.mapper.mapArray(elements, Floor, FloorResponseBody),
};
return res
.set({
'Content-Range': `elements ${offset}-${offset + limit}/${count}`,
})
.json(body);
return res.set({ 'Content-Range': `elements ${offset}-${offset + limit}/${count}` }).json(body);
}
}
4 changes: 4 additions & 0 deletions core/src/controllers/floors/models/floor.response.body.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ApiProperty } from '@nestjs/swagger';
import { PublishedState } from '../../../entities/base.published.entity';

export class FloorResponseBody {
@ApiProperty()
Expand All @@ -10,6 +11,9 @@ export class FloorResponseBody {
@ApiProperty()
description: string;

@ApiProperty({ enum: PublishedState })
status: PublishedState;

@ApiProperty()
createdBy: string;

Expand Down
4 changes: 4 additions & 0 deletions core/src/controllers/floors/models/mapper.profiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export class FloorMapperProfiles extends AutomapperProfile {
(d) => d.description,
mapFrom((s) => s.description),
),
forMember(
(d) => d.status,
mapFrom((s) => s.status),
),
forMember(
(d) => d.createdBy,
mapFrom((s) => s.createdBy.toString()),
Expand Down
8 changes: 8 additions & 0 deletions core/src/entities/floors/floors.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ export class FloorsService extends BaseEntityService {
}
}

async deleteFloor(floorId: string): Promise<Floor | undefined> {
try {
return await this.FloorModel.findByIdAndDelete(floorId);
} catch {
return null;
}
}

async findOneById(id: string): Promise<Floor | undefined> {
return await this.FloorModel.findById(id);
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/entities/floors/models/floor.entity.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';
import { BaseEntity } from '../../base.entity';
import { BasePublishedEntity } from '../../base.published.entity';

export type FloorDocument = Floor & Document;

@Schema()
export class Floor extends BaseEntity {
export class Floor extends BasePublishedEntity {
@Prop({ required: true, unique: true })
name: string;

Expand Down

0 comments on commit 942b897

Please sign in to comment.