Skip to content

Commit

Permalink
add and delete live item api
Browse files Browse the repository at this point in the history
  • Loading branch information
于占峰 committed Aug 5, 2024
1 parent f71030c commit 195563a
Show file tree
Hide file tree
Showing 15 changed files with 292 additions and 19 deletions.
16 changes: 12 additions & 4 deletions rust-src/api/src/controller/live_prize_pool_item_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ async fn info(app_state: web::Data<AppState>,
Ok(HttpResponse::Ok().json(JsonResult::ok(res)))
}

#[post("/api/live_prize_pool_item/add/{live_id}/{item_id}")]
#[get("/api/live_prize_pool_item/add/{live_id}/{item_id}")]
async fn add(app_state: web::Data<AppState>,
live_id: web::Path<i64>,
item_id: web::Path<i64>,) -> Result<HttpResponse, MyError> {
let res = live_prize_pool_item_service::add(&app_state.db, live_id.into_inner(), item_id.into_inner()).await?;
param: web::Path<(i64, i64)>,) -> Result<HttpResponse, MyError> {
let param = param.into_inner();
let res = live_prize_pool_item_service::add(&app_state.db, param.0, param.1).await?;
Ok(HttpResponse::Ok().json(JsonResult::ok(res)))
}

Expand All @@ -41,3 +41,11 @@ async fn update(app_state: web::Data<AppState>,
let res = live_prize_pool_item_service::update(&app_state.db, form.into_inner()).await?;
Ok(HttpResponse::Ok().json(JsonResult::ok(res)))
}

#[get("/api/live_prize_pool_item/delete/{id}")]
async fn delete(app_state: web::Data<AppState>,
params: web::Path<i64>, ) -> Result<HttpResponse, MyError> {
let res = live_prize_pool_item_service::delete(&app_state.db, params.into_inner()).await?;
Ok(HttpResponse::Ok().json(JsonResult::ok(res)))
}

6 changes: 6 additions & 0 deletions rust-src/api/src/controller/prize_pool_item_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,10 @@ async fn delete(app_state: web::Data<AppState>,
params: web::Path<i64>, ) -> Result<HttpResponse, MyError> {
let res = prize_pool_item_service::delete(&app_state.db, params.into_inner()).await?;
Ok(HttpResponse::Ok().json(JsonResult::ok(res)))
}
#[get("/api/prize_pool_item/get_by_pool_id/{pool_id}")]
async fn get_by_pool_id(app_state: web::Data<AppState>,
params: web::Path<i64>, ) -> Result<HttpResponse, MyError> {
let res = prize_pool_item_service::get_by_pool_id(&app_state.db, params.into_inner()).await?;
Ok(HttpResponse::Ok().json(JsonResult::ok(res)))
}
7 changes: 5 additions & 2 deletions rust-src/api/src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ pub fn live_prize_pool_item_routes(cfg: &mut web::ServiceConfig) {
cfg.service(live_prize_pool_item_controller::list)
.service(live_prize_pool_item_controller::update)
.service(live_prize_pool_item_controller::info)
.service(live_prize_pool_item_controller::page);
.service(live_prize_pool_item_controller::page)
.service(live_prize_pool_item_controller::delete)
.service(live_prize_pool_item_controller::add);
}

pub fn live_prize_pool_routes(cfg: &mut web::ServiceConfig) {
Expand All @@ -68,7 +70,8 @@ pub fn prize_pool_item_routes(cfg: &mut web::ServiceConfig) {
.service(prize_pool_item_controller::update)
.service(prize_pool_item_controller::delete)
.service(prize_pool_item_controller::info)
.service(prize_pool_item_controller::page);
.service(prize_pool_item_controller::page)
.service(prize_pool_item_controller::get_by_pool_id);
}

pub fn prize_pool_routes(cfg: &mut web::ServiceConfig) {
Expand Down
2 changes: 2 additions & 0 deletions rust-src/migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod m20240718_000001_create_table;
mod m20240719_000001_create_table;
mod m20240721_000001_create_table;
mod m20240722_000001_create_table;
mod m20240805_000001_create_table;


pub struct Migrator;
Expand All @@ -39,6 +40,7 @@ impl MigratorTrait for Migrator {
Box::new(m20240719_000001_create_table::Migration),
Box::new(m20240721_000001_create_table::Migration),
Box::new(m20240722_000001_create_table::Migration),
Box::new(m20240805_000001_create_table::Migration),
]
}
}
10 changes: 5 additions & 5 deletions rust-src/migration/src/m20240719_000001_create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl MigrationTrait for Migration {
resource_action: Set(Some(true)),
order_number: Set(Some(0)),
url: Default::default(),
api_path: Set(Some("/api/prize_pool_item/info/".to_string())),
api_path: Set(Some("/api/live_prize_pool_item/info/".to_string())),
api_http_method: Set(Some("GET".to_string())),
api_path_regex: NotSet,
role: NotSet,
Expand All @@ -148,7 +148,7 @@ impl MigrationTrait for Migration {
resource_action: Set(Some(true)),
order_number: Set(Some(0)),
url: Default::default(),
api_path: Set(Some("/api/prize_pool_item/list".to_string())),
api_path: Set(Some("/api/live_prize_pool_item/list".to_string())),
api_http_method: Set(Some("GET".to_string())),
api_path_regex: NotSet,
role: NotSet,
Expand All @@ -166,7 +166,7 @@ impl MigrationTrait for Migration {
resource_action: Set(Some(true)),
order_number: Set(Some(0)),
url: Default::default(),
api_path: Set(Some("/api/prize_pool_item/page".to_string())),
api_path: Set(Some("/api/live_prize_pool_item/page".to_string())),
api_http_method: Set(Some("POST".to_string())),
api_path_regex: NotSet,
role: NotSet,
Expand All @@ -184,7 +184,7 @@ impl MigrationTrait for Migration {
resource_action: Set(Some(true)),
order_number: Set(Some(0)),
url: Default::default(),
api_path: Set(Some("/api/prize_pool_item/add".to_string())),
api_path: Set(Some("/api/live_prize_pool_item/add".to_string())),
api_http_method: Set(Some("GET".to_string())),
api_path_regex: NotSet,
role: NotSet,
Expand All @@ -202,7 +202,7 @@ impl MigrationTrait for Migration {
resource_action: Set(Some(true)),
order_number: Set(Some(0)),
url: Default::default(),
api_path: Set(Some("/api/prize_pool_item/update".to_string())),
api_path: Set(Some("/api/live_prize_pool_item/update".to_string())),
api_http_method: Set(Some("POST".to_string())),
api_path_regex: NotSet,
role: NotSet,
Expand Down
44 changes: 44 additions & 0 deletions rust-src/migration/src/m20240805_000001_create_table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use sea_orm_migration::prelude::*;

use entity::{resource, role_resource};

use crate::sea_orm::{ActiveModelTrait, NotSet};
use crate::sea_orm::ActiveValue::Set;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
let db = manager.get_connection();
resource::ActiveModel {
id: Set(62),
parent_id: Set(48),
resource_name: Set(Some("删除活动奖池物品".to_string())),
resource_code: Set(Some("api_live_prize_pool_item_delete".to_string())),
resource_type: Set(Some(2)),
resource_root: Set(Some(false)),
resource_action: Set(Some(true)),
order_number: Set(Some(0)),
url: Default::default(),
api_path: Set(Some("/api/live_prize_pool_item/delete".to_string())),
api_http_method: Set(Some("GET".to_string())),
api_path_regex: NotSet,
role: NotSet,
status: Set(Some(true)),
icon: NotSet,
resource_desc: Set(Some("删除活动奖池物品接口".to_string())),
}.insert(db).await?;
role_resource::ActiveModel {
id: NotSet,
role_id: Set(1),
resource_id: Set(62),
}.insert(db).await?;
Ok(())
}

async fn down(&self, _manager: &SchemaManager) -> Result<(), DbErr> {
Ok(())
}
}
10 changes: 7 additions & 3 deletions rust-src/service/src/live_prize_pool_item_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ pub async fn page(db: &DbConn, page: LivePrizePoolItemPage) -> Result<PageResult
live_prize_pool_item_manager::page(db, page).await
}

pub async fn add(_db: &DbConn, _live_id: i64, _item_id: i64) -> Result<i64, MyError> {
Ok(1)
}
pub async fn add(db: &DbConn, live_id: i64, item_id: i64) -> Result<i64, MyError> {
live_prize_pool_item_manager::add_for_pool_item(db,live_id,item_id).await
}

pub async fn delete(db: &DbConn, id: i64) -> Result<bool, MyError> {
Ok(live_prize_pool_item_manager::delete(db, id).await?)
}
20 changes: 19 additions & 1 deletion rust-src/service/src/manager/live_prize_pool_item_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ use entity::{live_prize_pool_item, prize_pool_item};
use entity::live_prize_pool_item::Model;
use entity::prelude::LivePrizePoolItem;
use model::prize::LivePrizePoolItemPage;
use crate::manager::prize_pool_item_manager::check_probability;

use crate::manager::prize_pool_item_manager;
use crate::manager::prize_pool_item_manager::check_probability;

pub async fn create_live_item(db: &DbConn, live_id : i64, items: Vec<prize_pool_item::Model>) -> Result<i64, MyError> {
let mut entities = Vec::new();
Expand Down Expand Up @@ -215,4 +216,21 @@ pub async fn pool_item_remaining_quantity_count(db: &DbConn, live_id: i64) -> R
}
}
}
}

pub async fn add_for_pool_item(db: &DbConn, live_id: i64, item_id: i64) -> Result<i64, MyError> {
let item = prize_pool_item_manager::get_prize_pool_item_data(db, item_id).await?;
match item {
None => {
Err(MyError::ServerError("奖品不存在".to_string()))
}
Some(item) => {
create_live_item(db, live_id, vec![item]).await
}
}
}

pub async fn delete(db: &DbConn, id: i64) -> Result<bool, MyError> {
let res = LivePrizePoolItem::delete_by_id(id).exec(db).await?;
Ok(res.rows_affected == 1)
}
3 changes: 3 additions & 0 deletions rust-src/service/src/prize_pool_item_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ pub async fn page(db: &DbConn, page: PrizePoolItemPage) -> Result<PageResult<pri
prize_pool_item_manager::page(db, page).await
}

pub async fn get_by_pool_id(db: &DbConn, pool_id: i64) -> Result<Vec<prize_pool_item::Model>, MyError> {
prize_pool_item_manager::get_prize_pool_item_by_pool_id(db,pool_id).await
}
2 changes: 2 additions & 0 deletions web/src/api/livePrizePoolItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ export const getLivePrizePoolItemList = () => get("/api/live_prize_pool_item/lis
export const getLivePrizePoolItemInfo = (id) => get("/api/live_prize_pool_item/info/" + id);
export const getLivePrizePoolItemPage = (param) => postRequestBody("/api/live_prize_pool_item/page", param);
export const updateLivePrizePoolItem = (param) => postRequestBody("/api/live_prize_pool_item/update", param);
export const removeLivePrizePoolItem = (id) => get("/api/live_prize_pool_item/delete/" + id);
export const addLivePrizePoolItem = (live_id,item_id) => get("/api/live_prize_pool_item/add/"+live_id+"/"+item_id);

2 changes: 1 addition & 1 deletion web/src/api/prizePoolItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ export const getPrizePoolItemInfo = (id) => get("/api/prize_pool_item/info/" + i
export const getPrizePoolItemPage = (param) => postRequestBody("/api/prize_pool_item/page", param);
export const addPrizePoolItem = (param) => postRequestBody("/api/prize_pool_item/add", param);
export const updatePrizePoolItem = (param) => postRequestBody("/api/prize_pool_item/update", param);

export const getPrizePoolItemBtPoolId = (pool_id) => get("/api/prize_pool_item/get_by_pool_id/" + pool_id);
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import React, { useContext, useEffect, useRef } from 'react';
import {
Form,
FormInstance,
Modal,
Notification,
Select,
} from '@arco-design/web-react';
import locale from './locale';
import useLocale from '@/utils/useLocale';
import { GlobalContext } from '@/context';
import FormItem from '@arco-design/web-react/es/Form/form-item';
import { addLivePrizePoolItem } from '@/api/livePrizePoolItem';
import { getPrizePoolItemBtPoolId } from '@/api/prizePoolItem';

function AddPage(props: {
visible;
setVisible;
livePrizePoolId;
callback: () => void;
}) {
const formRef = useRef<FormInstance>();

const { lang } = useContext(GlobalContext);

const t = useLocale(locale);

const [loading, setLoading] = React.useState(false);

const [itemSelect, setItemSelect] = React.useState([]);


const handleSubmit = () => {
formRef.current.validate().then((values) => {
setLoading(true);
addLivePrizePoolItem(props.livePrizePoolId, values.item_id)
.then((res) => {
const { success, message } = res.data;
if (success) {
Notification.success({ content: message, duration: 1000 });
props.setVisible(false);
props.callback();
}
})
.finally(() => {
setLoading(false);
});
});
};

useEffect(() => {
setLoading(true);
getPrizePoolItemBtPoolId(props.livePrizePoolId).then((res) => {
const { success, data } = res.data;
if (success) {
setItemSelect(data.map((item) => {
return {
label: item.prize_name,
value: item.id,
};
}));
}
}).finally(() => {
setLoading(false);
});

}, [props.visible]);

return (
<Modal
title={t['searchTable.operations.add']}
style={{ width: '35%' }}
visible={props.visible}
onOk={() => {
handleSubmit();
}}
onCancel={() => {
props.setVisible(false);
}}
autoFocus={false}
focusLock={true}
confirmLoading={loading}
>
<Form
ref={formRef}
style={{ width: '95%', marginTop: '6px' }}
labelCol={{ span: lang === 'en-US' ? 7 : 6 }}
wrapperCol={{ span: lang === 'en-US' ? 17 : 18 }}
>
<FormItem
required
label={t['searchTable.columns.item_id']}
field={'item_id'}
>
<Select
placeholder={t['searchForm.placeholder']}
options={itemSelect}
allowClear
/>
</FormItem>
</Form>
</Modal>
);
}

export default AddPage;
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,27 @@ export function getColumns(
{t['searchTable.columns.operations.update']}
</Button>
</PermissionWrapper>,
<PermissionWrapper
key={'delete'}
requiredPermissions={[
{
resource: 'live_prize_pool_item_manager',
actions: ['api_live_prize_pool_item_delete'],
},
]}
>
<Popconfirm
focusLock
title={t['option.delete.confirm.title']}
onOk={() => {
callback(record, 'delete');
}}
>
<Button status="danger" type="text" size="small">
{t['searchTable.columns.operations.delete']}
</Button>
</Popconfirm>
</PermissionWrapper>,
],
},
];
Expand Down
Loading

0 comments on commit 195563a

Please sign in to comment.