|
11 | 11 | import com.example.franchise_api.mapper.ProductMapper; |
12 | 12 | import com.example.franchise_api.model.Branch; |
13 | 13 | import com.example.franchise_api.dto.TopProductResponse; |
| 14 | +import com.example.franchise_api.dto.UpdateNameBranch; |
| 15 | +import com.example.franchise_api.dto.UpdateNameFranchise; |
| 16 | +import com.example.franchise_api.dto.UpdateNameProduct; |
14 | 17 | import com.example.franchise_api.model.Franchise; |
15 | 18 | import com.example.franchise_api.model.Product; |
16 | 19 | import com.example.franchise_api.repository.FranchiseRepository; |
@@ -59,7 +62,7 @@ private void validateFranchise(String franchiseName) { |
59 | 62 | } |
60 | 63 | } |
61 | 64 |
|
62 | | - public @Nullable Franchise addBranchToFranchises(BranchesCreateDto dto, String name) { |
| 65 | + public @Nullable Franchise addBranchToFranchise(BranchesCreateDto dto, String name) { |
63 | 66 | Branch branch = branchesMapper.toEntity(dto); |
64 | 67 | Query query = new Query(Criteria.where("name").is(name)); |
65 | 68 | Update update = new Update().push("branches", branch); |
@@ -126,4 +129,38 @@ public List<TopProductResponse> getTopStockProducts(String franchiseName) { |
126 | 129 | }).toList(); |
127 | 130 | } |
128 | 131 |
|
| 132 | + public @Nullable Franchise updateName(String franchiseName, UpdateNameFranchise franchiseUpdateName) { |
| 133 | + Query query = new Query(Criteria.where("name").is(franchiseName)); |
| 134 | + Update update = new Update().set("name", franchiseUpdateName.name()); |
| 135 | + Franchise franchise = mongoTemplate.findAndModify(query, update, FindAndModifyOptions.options().returnNew(true), |
| 136 | + Franchise.class); |
| 137 | + return Optional.ofNullable(franchise).orElseThrow(() -> new FranchiseNotFoundException(franchiseName)); |
| 138 | + |
| 139 | + } |
| 140 | + |
| 141 | + public Franchise updateNameBranch(String franchiseName, String branchName, UpdateNameBranch updateNameBranch) { |
| 142 | + this.validateFranchise(franchiseName); |
| 143 | + Query query = new Query(Criteria.where("name").is(franchiseName).and("branches.name").is(branchName)); |
| 144 | + Update update = new Update().set("branches.$.name", updateNameBranch.name()); |
| 145 | + Franchise franchise = mongoTemplate.findAndModify(query, update, FindAndModifyOptions.options().returnNew(true), |
| 146 | + Franchise.class); |
| 147 | + return Optional.ofNullable(franchise).orElseThrow(() -> new FranchiseNotFoundException(franchiseName)); |
| 148 | + } |
| 149 | + |
| 150 | + public Franchise updateNameProduct(String franchiseName, String branchName, String productName, |
| 151 | + UpdateNameProduct updateNameProduct) { |
| 152 | + this.validateFranchise(franchiseName); |
| 153 | + Query query = new Query(Criteria.where("name").is(franchiseName)); |
| 154 | + |
| 155 | + Update update = new Update().set("branches.$[branch].products.$[product].name", updateNameProduct.name()) |
| 156 | + .filterArray(Criteria.where("branch.name").is(branchName)) |
| 157 | + .filterArray(Criteria.where("product.name").is(productName)); |
| 158 | + |
| 159 | + FindAndModifyOptions options = FindAndModifyOptions.options().returnNew(true); |
| 160 | + |
| 161 | + Franchise franchise = mongoTemplate.findAndModify(query, update, options, Franchise.class); |
| 162 | + return Optional.ofNullable(franchise).orElseThrow(() -> new FranchiseNotFoundException(franchiseName)); |
| 163 | + |
| 164 | + } |
| 165 | + |
129 | 166 | } |
0 commit comments