前言
本项目是为开发一套容器化的开发、运行、测试环境,用以支持Web开发、程序设计等课程的实验教学。
任务目标
实现通知的新建,查询,更新功能。
我的任务
Mapper层编写sql,操作数据库,建立通知的Service和controller层,编写通知的新建,查询,更新接口。
工作内容
建立Message表,设计对应操作API
代码
实体类
Message.java
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Message {
private Integer id;
private Integer organizationId;
private String name;
private String description;
private Integer status;
private Date createTime;
private Date lastModifyTime;
}
lombok注解自动补全get,set方法。
MessageMapper.xml
<resultMap id="baseMessageMap" type="cn.sdu.weblab.entity.Message">
<id column="id" property="id"/>
<result column="organization_id" property="organizationId"></result>
<result column="name" property="name"></result>
<result column="description" property="description"></result>
</resultMap>
<insert id="addMessage">
insert into message(id, organization_id, name, description, status)
values (#{id}, #{organizationId}, #{name}, #{description}, #{status})
</insert>
<update id="reNameById">
update message set name = #{name} where id=#{id} and status=0
</update>
<update id="reDescriptionById">
update message set description = #{desc} where id=#{id} and status=0
</update>
<update id="deleteMessageById">
update message set status=1 where id=#{id}
</update>
<update id="deleteMessageByName">
update message set status=1 where name=#{name} and organization_id=#{orgId}
</update>
<select id="getMessageById" resultMap="baseMessageMap">
select * from message
where id=#{id} and status=0
</select>
<select id="getMessageByName" resultMap="baseMessageMap">
select * from message
where name=#{name} and organization_id=#{orgId} and status=0
</select>
<select id="getOrgMessageList" resultMap="baseMessageMap">
select * from message
where organization_id=#{orgId} and status=0
</select>
MessageServiceImpl.java
@Service
public class MessageServiceImpl implements MessageService {
@Autowired
MessageMapper messageMapper;
/**
* 新建通知
* @param message
*/
@Override
public void addMessage(Message message) {
messageMapper.addMessage(message);
}
/**
* 通过ID查询通知
* @param id
* @return
*/
@Override
public Message getMessageById(int id) {
Message message = messageMapper.getMessageById(id);
return message;
}
/**
* 通过name查询通知
* @param name
* @return
*/
@Override
public List<Message> getMessageByName(String name, int orgId) {
List<Message> messagelist = messageMapper.getMessageByName(name, orgId);
return messagelist;
}
/**
* 通过ID修改name
* @param id
* @param name
*/
@Override
public void updateNameById(int id, String name) {
messageMapper.reNameById(id, name);
}
/**
* 通过ID修改描述
* @param id
* @param desc
*/
@Override
public void updateDescriptionById(int id, String desc) {
messageMapper.reDescriptionById(id, desc);
}
/**
* 通过ID删除通知
* @param id
*/
@Override
public void deleteMessageById(int id) {
messageMapper.deleteMessageById(id);
}
/**
* 通过name删除通知
* @param name
*/
@Override
public void deleteMessageByName(String name, int orgId) {
messageMapper.deleteMessageByName(name, orgId);
}
/**
* 查询通知列表
* @param orgId
* @return
*/
@Override
public List<Message> getMessageList(int orgId) {
List<Message> list = messageMapper.getOrgMessageList(orgId);
return list;
}
}
@RestController
@RequestMapping("/message")
@Slf4j
public class MessageController {
@Autowired
private MessageService messageService;
@Autowired
private OrganizationService organizationService;
/**
* 新建通知
* @param orgId
* @param name
* @param desc
* @return
* @throws Exception
*/
@PostMapping(value = "/createMessage")
public ResultEntity createMessage(int orgId, String name,String desc) throws Exception {
if(organizationService.getOrgById(orgId)==null)
return ResultEntity.error("Organization not found",null);
Message message = new Message();
message.setOrganizationId(orgId);
message.setId(Integer.parseInt(UserUtil.getVerifyCode(6)));
message.setStatus(0);
message.setName(name);
message.setDescription(desc);
System.out.println(message);
messageService.addMessage(message);
return ResultEntity.success(message.getId());
}
@PostMapping(value = "/getMessageById")
public ResultEntity getMessageById(int id){
Message message = messageService.getMessageById(id);
if(message==null)
return ResultEntity.error("Message not found",null);
if(organizationService.getOrgById(message.getOrganizationId())==null)
return ResultEntity.error("Organization not found",null);
return ResultEntity.success(message);
}
@PostMapping(value = "/getMessageByName")
public ResultEntity getMessageByName(String name, int orgId) {
if(organizationService.getOrgById(orgId)==null)
return ResultEntity.error("Organization not found",null);
List<Message> messagelist = messageService.getMessageByName(name,orgId);
return ResultEntity.success(messagelist);
}
@PostMapping(value = "/getOrgMessageList")
public ResultEntity getOrgMessageList(int orgId) {
if(organizationService.getOrgById(orgId)==null)
return ResultEntity.error("Organization not found",null);
List<Message> list = messageService.getMessageList(orgId);
return ResultEntity.success(list);
}
@PostMapping(value = "/updateNameById")
public ResultEntity updateNameById(int id, String name) {
if(messageService.getMessageById(id)==null)
return ResultEntity.error("Message not found",null);
if(organizationService.getOrgById(messageService.getMessageById(id).getOrganizationId())==null)
return ResultEntity.error("Organization not found",null);
messageService.updateNameById(id, name);
return ResultEntity.success(messageService.getMessageById(id));
}
@PostMapping(value = "/updateDescriptionById")
public ResultEntity updateDescriptionById(int id, String desc) {
if(messageService.getMessageById(id)==null)
return ResultEntity.error("Message not found",null);
if(organizationService.getOrgById(messageService.getMessageById(id).getOrganizationId())==null)
return ResultEntity.error("Organization not found",null);
messageService.updateDescriptionById(id, desc);
return ResultEntity.success(messageService.getMessageById(id));
}
@PostMapping(value = "/deleteMessageById")
public ResultEntity deleteMessageById(int id) {
if(messageService.getMessageById(id)==null)
return ResultEntity.error("Message not found",null);
if(organizationService.getOrgById(messageService.getMessageById(id).getOrganizationId())==null)
return ResultEntity.error("Organization not found",null);
messageService.deleteMessageById(id);
return ResultEntity.success();
}
}