-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLotService.java
More file actions
62 lines (49 loc) · 2.09 KB
/
Copy pathLotService.java
File metadata and controls
62 lines (49 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.example.cw.services;
import com.example.cw.dao.DAO;
import com.example.cw.dao.DAOFactory;
import com.example.cw.model.Customer;
import com.example.cw.model.Lot;
import com.example.cw.exceptions.DBException;
import javax.validation.constraints.DecimalMin;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class LotService implements BusinessService {
private final DAO<Lot> lotDAO;
public LotService(DAOFactory daoFactory) {
this.lotDAO = daoFactory.getDAO(Lot.class);
}
public List<Lot> getActiveLots(int pageNumber, int sizeLimit) {
return lotDAO.getAll(new Lot().setStatus(true), pageNumber, sizeLimit);
}
public Integer getSumOfRecords() {
return lotDAO.getAll(new Lot().setStatus(true)).size();
}
public List<Lot> getLotsWithOwner(long ownerID) {
return lotDAO.getAll(new Lot().setCustomer(new Customer().setId(ownerID)));
}
public List<Lot> getActiveLotsByName(String name) {
return Stream.concat(lotDAO.getAll(new Lot().setName(name).setStatus(true)).stream(),
lotDAO.getAll(new Lot().setDescription(name).setStatus(true)).stream())
.collect(Collectors.toList());
}
public Lot getLotById(long id) {
return lotDAO.get(id).orElseThrow(() -> new DBException("Failed to get lot"));
}
public void changeStatus(long lotId) {
Lot lot = lotDAO.get(lotId).orElseThrow(() -> new DBException("Failed to get lot"));
lotDAO.update(lot.setStatus(!lot.getIsActive()));
}
public void deleteLot(long lotId) {
lotDAO.delete(new Lot().setId(lotId));
}
public String generateURL() {
return UUID.randomUUID().toString();
}
public void createNewLot(Long ownerID, String name, String description,
@DecimalMin(value = "0.01", message = "Price must be at least 0.01 ") Double startPrice) {
lotDAO.save(new Lot(name, startPrice, false, new Customer().setId(ownerID))
.setDescription(description));
}
}