This is an automated email from the ASF dual-hosted git repository. mabin pushed a commit to branch houserush-sample in repository https://gitbox.apache.org/repos/asf/servicecomb-samples.git
commit ac82b4fc16e3a3acc6745a7051ed476ab16aaaa4 Author: KangJingbo <[email protected]> AuthorDate: Tue Jul 30 19:44:25 2019 +0800 houserush-realestate initial version --- houserush/realestate/README.md | 245 +++++++++++++++++++++ .../realestate/RealestateApplication.java | 42 ++++ .../houserush/realestate/RealestateConfig.java | 26 +++ .../houserush/realestate/aggregate/Building.java | 65 ++++++ .../houserush/realestate/aggregate/House.java | 66 ++++++ .../houserush/realestate/aggregate/Realestate.java | 59 +++++ .../houserush/realestate/api/RealestateApi.java | 155 +++++++++++++ .../realestate/api/RealestateApiRestImpl.java | 120 ++++++++++ .../houserush/realestate/dao/BuildingDao.java | 24 ++ .../houserush/realestate/dao/HouseDao.java | 32 +++ .../houserush/realestate/dao/HouseDaoImpl.java | 32 +++ .../houserush/realestate/dao/HouseDaoMore.java | 24 ++ .../houserush/realestate/dao/RealestateDao.java | 24 ++ .../realestate/service/RealestateService.java | 59 +++++ .../realestate/service/RealestateServiceImpl.java | 169 ++++++++++++++ .../src/main/resources/microservice.yaml | 43 ++++ 16 files changed, 1185 insertions(+) diff --git a/houserush/realestate/README.md b/houserush/realestate/README.md new file mode 100644 index 0000000..18835e5 --- /dev/null +++ b/houserush/realestate/README.md @@ -0,0 +1,245 @@ +## 微服务 realestate + +该微服务用于楼盘及房源管理 + +###快速开始 + +1、参考[ServiceComb快速入门](http://servicecomb.apache.org/cn/docs/quick-start/)安装开发环境: + +- 安装git,详情可参考[git安装教程](https://git-scm.com/book/zh/v2/%E8%B5%B7%E6%AD%A5-%E5%AE%89%E8%A3%85-Git)。 +- 安装JDK 1.8,详情可参考[JDK安装教程](https://docs.oracle.com/javase/8/docs/technotes/guides/install/install_overview.html)。 +- 安装Maven 3.x, 详情可参考[Maven安装教程](https://maven.apache.org/install.html)。 + +2、运行 Service Center + +- 安装Docker,详情可参考[Docker安装教程](https://www.docker.com/get-started)。 +- 命令行或终端输入<code>$ docker pull servicecomb/service-center</code>拉取最新版servicecomb/service-center。 +- 命令行或终端输入<code>$ docker run -d -p 30100:30100 servicecomb/service-center:latest</code>在30100端口运行service-center。 + +3、参考下文的[数据表设计](#dbDisign)创建数据库表。 + +4、配置realestate的数据库、服务中心地址以及启动端口 + +- 修改src/main/resources/microservice.yaml文件: +```yaml +#... +servicecomb: + service: + registry: + address: http://127.0.0.1:30100 #service-center地址 + rest: + address: 0.0.0.0:7771 #微服务启动端口 + #... +spring: + #数据库url、用户名、密码配置 + datasource: + url: jdbc:mysql://127.0.0.1:3306/realestate?characterEncoding=utf8&useSSL=false + username: houserush + password: password + #... +``` +5、启动微服务 +- 该微服务基于spring-boot开发,所以启动微服务只需要在微服务根目录下打开命令行或终端输入<code>$ mvn spring-boot:run</code>,或通过IDE启动spring-boot程序。 + +### 主要功能 + +- 楼盘的增删改查 + +- 建筑楼的增删改查 + +- 房源的增删改查 + +- 锁定已售房源 + +### 设计原理 + +一个楼盘可能含多栋建筑楼,一栋建筑楼可能含多个房源 + +### 数据表设计 +<span id = "dbDisign" /> + +- realestates + +| 字段 | 类型 | 描述 | +| :-------------- | ------------ | --------| +| id | int | 主键id | +| name | varchar(255) | 楼盘名称 | +| description | varchar(2048)| 描述 | +| deleted_at | timestamp | 删除时间 | +| created_at | timestamp | 创建时间 | +| update_at | timestamp | 更新时间 | + +- buildings + +| 字段 | 类型 | 描述 | +| :--------------------- | ------------ | --------- | +| id | int | 主键id | +| realestate_id | int | 所在楼盘id | +| name | varchar(255) | 建筑楼名称 | +| sequence_in_realestate | tinyint | 栋 | +| deleted_at | timestamp | 删除时间 | +| created_at | timestamp | 创建时间 | +| update_at | timestamp | 更新时间 | + +- houses + +| 字段 | 类型 | 描述 | +| :---------------- | ------------ | --------- | +| id | int | 主键id | +| building_id | int | 所在建筑楼id | +| name | varchar(255) | 房源名称 | +| layer | int | 房源所在楼层 | +| state | varchar(255) | 房源状态 | +| sequence_in_layer | tinyint | 房间号 | +| price | decimal | 价格 | +| deleted_at | timestamp | 删除时间 | +| created_at | timestamp | 创建时间 | +| update_at | timestamp | 更新时间 | + + +### 接口设计 + +```java +package org.apache.servicecomb.samples.practise.houserush.realestate.api; + +public interface RealestateApi { + /** + * 新增楼盘 + * @param realestate 楼盘信息 + * @return Realestate 添加成功后的楼盘信息 + */ + Realestate createRealestate(Realestate realestate); + + /** + * 查询楼盘 + * @param id 楼盘id + * @return Realestate 楼盘信息 + */ + Realestate findRealestate(int id); + + /** + * 修改楼盘信息 + * @param id 楼盘id + * @param realestate 楼盘信息 + * @return Realestate 修改成功后的楼盘信息 + */ + Realestate updateRealestate(int id, Realestate realestate); + + /** + * 删除楼盘 + * @param id 楼盘id + */ + void removeRealestate(int id); + + /** + * 查询所有楼盘 + * @return List<Realestate> 所有楼盘列表 + */ + List<Realestate> indexRealestates(); + + /** + * 新增建筑楼 + * @param realestateId 楼盘id + * @param building 建筑楼信息 + * @return Building 添加成功后的建筑楼信息 + */ + Building createBuilding(int realestateId, Building building); + + /** + * 查询建筑楼 + * @param id 建筑楼id + * @return Building 建筑楼信息 + */ + Building findBuilding(int id); + + /** + * 更改建筑楼信息 + * @param id 建筑楼id + * @param building 建筑楼信息 + * @return Building 更改成功后的建筑楼信息 + */ + Building updateBuilding(int id, Building building); + + /** + * 删除建筑楼 + * @param id 建筑楼id + */ + void removeBuilding(int id); + + /** + * 查询某一楼盘下的所有建筑楼 + * @param realestateId 楼盘id + * @return List<Building> 建筑楼列表 + */ + List<Building> indexBuildings(int realestateId); + + /** + * 新增房源信息 + * @param buidingId 建筑楼id + * @param house 房源信息 + * @return House 添加成功后的房源信息 + */ + House createHouse(int buidingId, House house); + + /** + * 查询房源信息 + * @param id 房源id + * @return House 房源信息 + */ + House findHouse(int id); + + /** + * 更改房源信息 + * @param id 房源id + * @param house 房源信息 + * @return House 更改成功后的房源信息 + */ + House updateHouse(int id, House house); + + /** + * 删除房源信息 + * @param id 房源id + */ + void removeHouse(int id); + + /** + * 查询某一建筑楼下的所有房源 + * @param buildingId 建筑楼id + * @return List<House> 所有房源列表 + */ + List<House> indexHouses(int buildingId); + + /** + * 锁定已售房源 + * @param ids 已售房源id列表 + * @return List<House> 锁定的房源列表 + */ + List<House> lockHousesForSale(List<Integer> ids); +} +``` + +### Rest API调用示例 +点击[houserush-realestate Rest API文档](https://documenter.getpostman.com/view/5023270/SVYkwMZd?version=latest)查看Rest API调用示例。 + +- 注意:示例中是通过Gateway网关服务调用的Realestate服务,所以示例调用接口较org.apache.servicecomb.samples.practise.houserush.realestate.api.RealestateApiRestImpl中声明的接口多含一个前缀/realestate. +- 鉴权操作、获取用户Token操作请移步[houserush-gateway服务文档](https://github.com/apache/servicecomb-samples/tree/master/houserush/gateway) + +### 源码文件解析 + +```yaml +src/main: + /java: java源码文件所在目录 + org.apache.servicecomb.samples.practise.houserush.realestate: + .aggregate: 项目实体类所在包,其中Building、House、Realestate类为JPA实体 + .api: Rest接口定义及实现包 + .RealestateApi: 接口定义 + .RealestateApiRestImpl: 接口实现 + .dao: 数据访问对象所在包,其中数据库操作都是Spring-Data-JPA实现 + .service: 各种增删改查具体逻辑实现 + .RealestateConfig: 微服务配置类 + .RealestateApplication: 微服务启动类 + /resources: 项目资源文件所在目录 + /microservice.yaml: 微服务配置文件,其中关键内容在上文"快速开始"中已经提及。 +pom.xml: maven配置文件 + +``` \ No newline at end of file diff --git a/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/RealestateApplication.java b/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/RealestateApplication.java new file mode 100644 index 0000000..0cfb114 --- /dev/null +++ b/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/RealestateApplication.java @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.servicecomb.samples.practise.houserush.realestate; + +import org.apache.servicecomb.common.rest.codec.RestObjectMapperFactory; +import org.apache.servicecomb.springboot.starter.provider.EnableServiceComb; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import java.text.SimpleDateFormat; +import java.util.TimeZone; + +@SpringBootApplication +@EnableServiceComb +public class RealestateApplication { + + public static void main(String[] args) { + configBeforeBoot(); + SpringApplication.run(RealestateApplication.class, args); + } + + private static void configBeforeBoot() { + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); + RestObjectMapperFactory.getRestObjectMapper().setDateFormat(simpleDateFormat); + } +} diff --git a/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/RealestateConfig.java b/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/RealestateConfig.java new file mode 100644 index 0000000..b4136ae --- /dev/null +++ b/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/RealestateConfig.java @@ -0,0 +1,26 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.servicecomb.samples.practise.houserush.realestate; + +import org.springframework.context.annotation.Configuration; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; + +@Configuration +@EnableJpaRepositories(basePackages = "org.apache.servicecomb.samples.practise.houserush") +public class RealestateConfig { +} diff --git a/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/aggregate/Building.java b/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/aggregate/Building.java new file mode 100644 index 0000000..1c0a0c9 --- /dev/null +++ b/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/aggregate/Building.java @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.servicecomb.samples.practise.houserush.realestate.aggregate; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import lombok.Data; +import org.hibernate.annotations.SQLDelete; +import org.hibernate.annotations.Where; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedDate; + +import javax.persistence.*; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +@Data +@Entity +@Table(name = "buildings") +@SQLDelete(sql = "update buildings set deleted_at = now() where id = ?") +@Where(clause = "deleted_at is null") +public class Building { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private int id; + + @JsonIgnore + @ManyToOne + @JoinColumn(name = "realestate_id") + private Realestate realestate; + + @JsonIgnore + @OneToMany(mappedBy = "building") + private List<House> houses = new ArrayList<>(); + + private String name; + + private Integer sequenceInRealestate; + + @Temporal(TemporalType.TIMESTAMP) + private Date deletedAt; + + @CreatedDate + @Temporal(TemporalType.TIMESTAMP) + private Date createdAt; + + @LastModifiedDate + @Temporal(TemporalType.TIMESTAMP) + private Date updatedAt; +} diff --git a/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/aggregate/House.java b/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/aggregate/House.java new file mode 100644 index 0000000..3c47904 --- /dev/null +++ b/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/aggregate/House.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.servicecomb.samples.practise.houserush.realestate.aggregate; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import lombok.Data; +import org.hibernate.annotations.SQLDelete; +import org.hibernate.annotations.Where; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedDate; + +import javax.persistence.*; +import java.math.BigDecimal; +import java.util.Date; + +@Data +@Entity +@Table(name = "houses") +@SQLDelete(sql = "update houses set deleted_at = now() where id = ?") +@Where(clause = "deleted_at is null") +public class House { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private int id; + + @JsonIgnore + @ManyToOne + @JoinColumn(name = "building_id") + private Building building; + + private String name; + + private Integer layer; + + private String state; + + private Integer sequenceInLayer; + + private BigDecimal price; + + @Temporal(TemporalType.TIMESTAMP) + private Date deletedAt; + + @CreatedDate + @Temporal(TemporalType.TIMESTAMP) + private Date createdAt; + + @LastModifiedDate + @Temporal(TemporalType.TIMESTAMP) + private Date updatedAt; +} diff --git a/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/aggregate/Realestate.java b/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/aggregate/Realestate.java new file mode 100644 index 0000000..f936ade --- /dev/null +++ b/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/aggregate/Realestate.java @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.servicecomb.samples.practise.houserush.realestate.aggregate; + +import lombok.Data; +import org.hibernate.annotations.SQLDelete; +import org.hibernate.annotations.Where; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedDate; + +import javax.persistence.*; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +@Data +@Entity +@Table(name = "realestates") +@SQLDelete(sql = "update realestates set deleted_at = now() where id = ?") +@Where(clause = "deleted_at is null") +public class Realestate { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private int id; + + @OneToMany(mappedBy = "realestate") + private List<Building> buildings = new ArrayList<>(); + + private String name; + + private String description; + + @Temporal(TemporalType.TIMESTAMP) + private Date deletedAt; + + @CreatedDate + @Temporal(TemporalType.TIMESTAMP) + private Date createdAt; + + @LastModifiedDate + @Temporal(TemporalType.TIMESTAMP) + private Date updatedAt; + +} diff --git a/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/api/RealestateApi.java b/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/api/RealestateApi.java new file mode 100644 index 0000000..ccef9c9 --- /dev/null +++ b/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/api/RealestateApi.java @@ -0,0 +1,155 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.servicecomb.samples.practise.houserush.realestate.api; + +import org.apache.servicecomb.samples.practise.houserush.realestate.aggregate.Building; +import org.apache.servicecomb.samples.practise.houserush.realestate.aggregate.House; +import org.apache.servicecomb.samples.practise.houserush.realestate.aggregate.Realestate; + +import java.util.List; + +public interface RealestateApi { + /** + * 新增楼盘 + * + * @param realestate 楼盘信息 + * @return Realestate 添加成功后的楼盘信息 + */ + Realestate createRealestate(Realestate realestate); + + /** + * 查询楼盘 + * + * @param id 楼盘id + * @return Realestate 楼盘信息 + */ + Realestate findRealestate(int id); + + /** + * 修改楼盘信息 + * + * @param id 楼盘id + * @param realestate 楼盘信息 + * @return Realestate 修改成功后的楼盘信息 + */ + Realestate updateRealestate(int id, Realestate realestate); + + /** + * 删除楼盘 + * + * @param id 楼盘id + */ + void removeRealestate(int id); + + /** + * 查询所有楼盘 + * + * @return List<Realestate> 所有楼盘列表 + */ + List<Realestate> indexRealestates(); + + /** + * 新增建筑楼 + * + * @param realestateId 楼盘id + * @param building 建筑楼信息 + * @return Building 添加成功后的建筑楼信息 + */ + Building createBuilding(int realestateId, Building building); + + /** + * 查询建筑楼 + * + * @param id 建筑楼id + * @return Building 建筑楼信息 + */ + Building findBuilding(int id); + + /** + * 更改建筑楼信息 + * + * @param id 建筑楼id + * @param building 建筑楼信息 + * @return Building 更改成功后的建筑楼信息 + */ + Building updateBuilding(int id, Building building); + + /** + * 删除建筑楼 + * + * @param id 建筑楼id + */ + void removeBuilding(int id); + + /** + * 查询某一楼盘下的所有建筑楼 + * + * @param realestateId 楼盘id + * @return List<Building> 建筑楼列表 + */ + List<Building> indexBuildings(int realestateId); + + /** + * 新增房源信息 + * + * @param buidingId 建筑楼id + * @param house 房源信息 + * @return House 添加成功后的房源信息 + */ + House createHouse(int buidingId, House house); + + /** + * 查询房源信息 + * + * @param id 房源id + * @return House 房源信息 + */ + House findHouse(int id); + + /** + * 更改房源信息 + * + * @param id 房源id + * @param house 房源信息 + * @return House 更改成功后的房源信息 + */ + House updateHouse(int id, House house); + + /** + * 删除房源信息 + * + * @param id 房源id + */ + void removeHouse(int id); + + /** + * 查询某一建筑楼下的所有房源 + * + * @param buildingId 建筑楼id + * @return List<House> 所有房源列表 + */ + List<House> indexHouses(int buildingId); + + /** + * 锁定已售房源 + * + * @param ids 已售房源id列表 + * @return List<House> 锁定的房源列表 + */ + List<House> lockHousesForSale(List<Integer> ids); +} \ No newline at end of file diff --git a/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/api/RealestateApiRestImpl.java b/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/api/RealestateApiRestImpl.java new file mode 100644 index 0000000..482ead8 --- /dev/null +++ b/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/api/RealestateApiRestImpl.java @@ -0,0 +1,120 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.servicecomb.samples.practise.houserush.realestate.api; + +import org.apache.servicecomb.provider.rest.common.RestSchema; +import org.apache.servicecomb.samples.practise.houserush.realestate.aggregate.Building; +import org.apache.servicecomb.samples.practise.houserush.realestate.aggregate.House; +import org.apache.servicecomb.samples.practise.houserush.realestate.aggregate.Realestate; +import org.apache.servicecomb.samples.practise.houserush.realestate.service.RealestateService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestSchema(schemaId = "realestateApiRest") +@RequestMapping("/") +public class RealestateApiRestImpl implements RealestateApi { + + @Autowired + private RealestateService realestateService; + + + @PostMapping("/realestates") + public Realestate createRealestate(@RequestBody Realestate realestate) { + return realestateService.createRealesate(realestate); + } + + @GetMapping("/realestates/{id}") + public Realestate findRealestate(@PathVariable("id") int id) { + return realestateService.findRealestate(id); + } + + @PutMapping("realestates/{id}") + public Realestate updateRealestate(@PathVariable("id") int id, @RequestBody Realestate realestate) { + realestate.setId(id); + return realestateService.updateRealestate(realestate); + } + + @DeleteMapping("realestates/{id}") + public void removeRealestate(@PathVariable("id") int id) { + realestateService.removeRealestate(id); + } + + @GetMapping("realestates") + public List<Realestate> indexRealestates() { + return realestateService.indexRealestates(); + } + + @PostMapping("realestates/{realestateId}/buildings") + public Building createBuilding(@PathVariable("realestateId") int realestateId, Building building) { + return realestateService.createBuilding(realestateId, building); + } + + @GetMapping("buildings/{id}") + public Building findBuilding(@PathVariable("id") int id) { + return realestateService.findBuilding(id); + } + + @PutMapping("buildings/{id}") + public Building updateBuilding(@PathVariable("id") int id, @RequestBody Building building) { + building.setId(id); + return realestateService.updateBuilding(building); + } + + @DeleteMapping("buildings/{id}") + public void removeBuilding(@PathVariable("id") int id) { + realestateService.removeBuilding(id); + } + + @GetMapping("realestates/{realestateId}/buildings") + public List<Building> indexBuildings(@PathVariable("realestateId") int realestateId) { + return realestateService.indexBuildings(realestateId); + } + + @PostMapping("buildings/{buildingId}/houses") + public House createHouse(@PathVariable("buildingId") int buildingId, House house) { + return realestateService.createHouse(buildingId, house); + } + + @GetMapping("houses/{id}") + public House findHouse(@PathVariable("id") int id) { + return realestateService.findHouse(id); + } + + @PutMapping("houses/{id}") + public House updateHouse(@PathVariable("id") int id, House house) { + house.setId(id); + return realestateService.updateHouse(house); + } + + @DeleteMapping("houses/{id}") + public void removeHouse(@PathVariable("id") int id) { + realestateService.removeHouse(id); + } + + @GetMapping("buildings/{buildingId}/houses") + public List<House> indexHouses(@PathVariable("buildingId") int buildingId) { + return realestateService.indexHouses(buildingId); + } + + @PutMapping("houses/lock_houses_for_sale") + public List<House> lockHousesForSale(@RequestBody List<Integer> ids) { + return realestateService.lockHousesForSale(ids); + } +} diff --git a/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/dao/BuildingDao.java b/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/dao/BuildingDao.java new file mode 100644 index 0000000..f728fdc --- /dev/null +++ b/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/dao/BuildingDao.java @@ -0,0 +1,24 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.servicecomb.samples.practise.houserush.realestate.dao; + +import org.apache.servicecomb.samples.practise.houserush.realestate.aggregate.Building; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface BuildingDao extends JpaRepository<Building, Integer> { +} diff --git a/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/dao/HouseDao.java b/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/dao/HouseDao.java new file mode 100644 index 0000000..93b6940 --- /dev/null +++ b/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/dao/HouseDao.java @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.servicecomb.samples.practise.houserush.realestate.dao; + +import org.apache.servicecomb.samples.practise.houserush.realestate.aggregate.House; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Lock; +import org.springframework.data.jpa.repository.Query; + +import javax.persistence.LockModeType; +import java.util.List; + +public interface HouseDao extends JpaRepository<House, Integer>, HouseDaoMore { + @Lock(LockModeType.PESSIMISTIC_WRITE) + @Query("SELECT h FROM House h WHERE h.id in (?1)") + List<House> findAllByIdInForUpdate(List<Integer> ids); +} diff --git a/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/dao/HouseDaoImpl.java b/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/dao/HouseDaoImpl.java new file mode 100644 index 0000000..aec75b3 --- /dev/null +++ b/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/dao/HouseDaoImpl.java @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.servicecomb.samples.practise.houserush.realestate.dao; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import java.util.List; + +public class HouseDaoImpl implements HouseDaoMore { + @PersistenceContext + private EntityManager em; + + @Override + public int updateLockingStatesForHouses(List<Integer> ids) { + return em.createQuery("UPDATE House h set h.state = 'locking' where h.id in (?1)").setParameter(1, ids).executeUpdate(); + } +} diff --git a/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/dao/HouseDaoMore.java b/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/dao/HouseDaoMore.java new file mode 100644 index 0000000..6b82347 --- /dev/null +++ b/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/dao/HouseDaoMore.java @@ -0,0 +1,24 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.servicecomb.samples.practise.houserush.realestate.dao; + +import java.util.List; + +public interface HouseDaoMore { + int updateLockingStatesForHouses(List<Integer> ids); +} diff --git a/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/dao/RealestateDao.java b/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/dao/RealestateDao.java new file mode 100644 index 0000000..2df74e3 --- /dev/null +++ b/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/dao/RealestateDao.java @@ -0,0 +1,24 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.servicecomb.samples.practise.houserush.realestate.dao; + +import org.apache.servicecomb.samples.practise.houserush.realestate.aggregate.Realestate; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface RealestateDao extends JpaRepository<Realestate, Integer> { +} diff --git a/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/service/RealestateService.java b/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/service/RealestateService.java new file mode 100644 index 0000000..525a797 --- /dev/null +++ b/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/service/RealestateService.java @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.servicecomb.samples.practise.houserush.realestate.service; + +import org.apache.servicecomb.samples.practise.houserush.realestate.aggregate.Building; +import org.apache.servicecomb.samples.practise.houserush.realestate.aggregate.House; +import org.apache.servicecomb.samples.practise.houserush.realestate.aggregate.Realestate; + +import java.util.List; + +public interface RealestateService { + Realestate createRealesate(Realestate realestate); + + Realestate findRealestate(Integer id); + + Realestate updateRealestate(Realestate realestate); + + void removeRealestate(Integer id); + + List<Realestate> indexRealestates(); + + Building createBuilding(Integer realestateId, Building building); + + Building findBuilding(Integer id); + + Building updateBuilding(Building building); + + void removeBuilding(Integer id); + + List<Building> indexBuildings(Integer realestateId); + + House createHouse(Integer buildingId, House house); + + House findHouse(Integer id); + + House updateHouse(House house); + + void removeHouse(Integer id); + + List<House> indexHouses(Integer buildingId); + + List<House> lockHousesForSale(List<Integer> houseIds); + +} diff --git a/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/service/RealestateServiceImpl.java b/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/service/RealestateServiceImpl.java new file mode 100644 index 0000000..85e5ccf --- /dev/null +++ b/houserush/realestate/src/main/java/org/apache/servicecomb/samples/practise/houserush/realestate/service/RealestateServiceImpl.java @@ -0,0 +1,169 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.servicecomb.samples.practise.houserush.realestate.service; + +import org.apache.servicecomb.samples.practise.houserush.realestate.aggregate.Building; +import org.apache.servicecomb.samples.practise.houserush.realestate.aggregate.House; +import org.apache.servicecomb.samples.practise.houserush.realestate.aggregate.Realestate; +import org.apache.servicecomb.samples.practise.houserush.realestate.dao.BuildingDao; +import org.apache.servicecomb.samples.practise.houserush.realestate.dao.HouseDao; +import org.apache.servicecomb.samples.practise.houserush.realestate.dao.RealestateDao; +import org.apache.servicecomb.swagger.invocation.exception.InvocationException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DataRetrievalFailureException; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +@Service +public class RealestateServiceImpl implements RealestateService { + @Autowired + private RealestateDao realestateDao; + + @Autowired + private BuildingDao buildingDao; + + @Autowired + private HouseDao houseDao; + + @Override + public Realestate createRealesate(Realestate realestate) { + return realestateDao.save(realestate); + } + + @Override + public Realestate findRealestate(Integer id) { + return realestateDao.findOne(id); + } + + @Override + @Transactional + public Realestate updateRealestate(Realestate realestate) { + int id = realestate.getId(); + if (realestateDao.exists(id)) { + return realestateDao.save(realestate); + } else { + throw new DataRetrievalFailureException("cannot update the non-existed realestate"); + } + } + + @Override + public void removeRealestate(Integer id) { + realestateDao.delete(id); + } + + @Override + public List<Realestate> indexRealestates() { + return realestateDao.findAll(); + } + + @Override + public Building createBuilding(Integer realestateId, Building building) { + Realestate realestate = realestateDao.findOne(realestateId); + if (null == realestate) { + throw new DataRetrievalFailureException("cannot create buildings for" + + " the not-existed realestate"); + } else { + building.setRealestate(realestate); + return buildingDao.save(building); + } + } + + @Override + public Building findBuilding(Integer id) { + return buildingDao.findOne(id); + } + + @Override + @Transactional + public Building updateBuilding(Building building) { + int id = building.getId(); + if (buildingDao.exists(id)) { + return buildingDao.save(building); + } else { + throw new DataRetrievalFailureException("cannot update the non-existed building"); + } + } + + @Override + @Transactional + public void removeBuilding(Integer id) { + buildingDao.delete(id); + } + + @Override + public List<Building> indexBuildings(Integer realestateId) { + Realestate realestate = realestateDao.findOne(realestateId); + return realestate.getBuildings(); + } + + @Override + public House createHouse(Integer buildingId, House house) { + Building building = buildingDao.findOne(buildingId); + if (building != null) { + house.setBuilding(building); + return houseDao.save(house); + } else { + throw new DataRetrievalFailureException("cannot create house for the non-existed building"); + } + } + + @Override + public House findHouse(Integer id) { + return houseDao.findOne(id); + } + + @Override + public House updateHouse(House house) { + int id = house.getId(); + if (houseDao.exists(id)) { + return houseDao.save(house); + } else { + throw new DataRetrievalFailureException("cannot update the non-existed house"); + } + } + + @Override + public void removeHouse(Integer id) { + houseDao.delete(id); + } + + @Override + public List<House> indexHouses(Integer buildingId) { + Building building = buildingDao.findOne(buildingId); + if (building != null) { + return building.getHouses(); + } else { + throw new DataRetrievalFailureException("cannot index the houses for the non-existed building"); + } + } + + @Override + @Transactional + public List<House> lockHousesForSale(List<Integer> houseIds) { + List<House> houses = houseDao.findAllByIdInForUpdate(houseIds); + houses.forEach(house -> { + if (!"in_stock".equals(house.getState())) { + throw new InvocationException(400, "", "house " + house.getId() + " is not in_stock."); + } + }); + houseDao.updateLockingStatesForHouses(houseIds); + return houses; + } +} diff --git a/houserush/realestate/src/main/resources/microservice.yaml b/houserush/realestate/src/main/resources/microservice.yaml new file mode 100644 index 0000000..5b20894 --- /dev/null +++ b/houserush/realestate/src/main/resources/microservice.yaml @@ -0,0 +1,43 @@ +# +## --------------------------------------------------------------------------- +## Licensed to the Apache Software Foundation (ASF) under one or more +## contributor license agreements. See the NOTICE file distributed with +## this work for additional information regarding copyright ownership. +## The ASF licenses this file to You under the Apache License, Version 2.0 +## (the "License"); you may not use this file except in compliance with +## the License. You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## --------------------------------------------------------------------------- + +# all interconnected microservices must belong to an application wth the same ID +APPLICATION_ID: houserush +service_description: + # name of the declaring microservice + name: realestate + version: 0.0.11 +servicecomb: + service: + registry: + address: http://127.0.0.1:30100 + rest: + address: 0.0.0.0:7771 + handler: + chain: + Provider: + default: bizkeeper-provider +spring: + datasource: + url: jdbc:mysql://127.0.0.1:3306/realestate?characterEncoding=utf8&useSSL=false + username: root + password: root + jpa: + properties: + hibernate: + enable_lazy_load_no_trans: true \ No newline at end of file
