Skip to content

Commit

Permalink
Merge pull request #27 from SWEN900072022/dev-backend
Browse files Browse the repository at this point in the history
Dev backend
  • Loading branch information
Blackmesa-Canteen authored Oct 19, 2022
2 parents b7f2b91 + e0bc2e5 commit 7740290
Show file tree
Hide file tree
Showing 27 changed files with 1,119 additions and 247 deletions.
89 changes: 89 additions & 0 deletions log/runtime.log.2022-10-07

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public interface IHotelBlo {
* edit a hotel of one hotelier
* @param hotelierId hotelier person id
*/
void editOwnedHotel(String hotelierId, HotelParam hotelParam);
void editOwnedHotel(String hotelierId, UpdateHotelParam hotelParam);

void editOwnedHotelWithLock(String hotelierId, HotelParam hotelParam);
void editOwnedHotelWithLock(String hotelierId, UpdateHotelParam hotelParam);

/**
* update a hotel info by hotel id
Expand Down Expand Up @@ -91,6 +91,8 @@ public interface IHotelBlo {
*/
List<HotelVo> searchHotelsByPageByPostCode(String currencyName, Integer pageNum, Integer pageSize, String postCode, Integer sortBy, Integer order);

void editOwnedHotelV(String hotelierId, UpdateHotelParam hotelParam);

/**
* get all hotels, may be used in admin
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface IRoomBlo {

void doUpdateRoom(UpdateRoomParam param);

void doUpdateRoomWithLock(UpdateRoomParam param);
void doUpdateRoomWithLock(UpdateRoomParam param, String userId);

void doDeleteRoomByRoomId(String RoomId);

Expand All @@ -26,7 +26,7 @@ public interface IRoomBlo {
*/
Room getRoomEntityByRoomId(String roomId);

Room getRoomEntityByRoomIdWithLock(String roomId);
Room getRoomEntityByRoomIdWithLock(String roomId, String userId);

/**
* get room vo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface ITransactionBlo {

void doUpdateBooking(String transactionId, String roomOrderId, int newQuantity);

void doUpdateBookingWithLock(String transactionId, String roomOrderId, int newQuantity);
// void doUpdateBookingWithLock(String transactionId, String roomOrderId, int newQuantity);

void doCancelBooking(String transactionId);

Expand All @@ -37,4 +37,8 @@ public interface ITransactionBlo {
List<TransactionVo> getAllTransactionsForHotelierId(String hotelierId, String currencyName);

List<TransactionVo> getAllTransactionsForHotelierIdWithStatusCode(String hotelierId, Integer statusCode, String currencyName);

void doMakeBookingWithRoomLock(String customerId, String hotelId, Date start, Date end, Map<String, Integer> roomIdNumberMap);

void doMakeBookingWithRoomVersionAndLock(Integer version, String customerId, String hotelId, Date start, Date end, Map<String, Integer> roomIdNumberMap);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@
import io.swen90007sm2.app.db.helper.UnitOfWorkHelper;
import io.swen90007sm2.app.lock.IResourceUserLockManager;
import io.swen90007sm2.app.lock.constant.LockConstant;
import io.swen90007sm2.app.lock.exception.ResourceConflictException;
import io.swen90007sm2.app.model.entity.Hotel;
import io.swen90007sm2.app.model.entity.HotelAmenity;
import io.swen90007sm2.app.model.entity.Hotelier;
import io.swen90007sm2.app.model.entity.Room;
import io.swen90007sm2.app.model.param.HotelParam;
import io.swen90007sm2.app.model.param.UpdateHotelParam;
import io.swen90007sm2.app.model.param.UpdateRoomParam;
import io.swen90007sm2.app.model.pojo.Money;
import io.swen90007sm2.app.model.vo.HotelVo;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -64,33 +66,81 @@ public class HotelBlo implements IHotelBlo {
@Qualifier(name = LockConstant.EXCLUSIVE_LOCK_MANAGER)
IResourceUserLockManager exclusiveLockManager;


/**
* optimistic lock solution
*/
@Override
public void editOwnedHotelWithLock(String hotelierId, HotelParam hotelParam) {
public void editOwnedHotel(String hotelierId, UpdateHotelParam hotelParam) {

String hotelId = hotelParam.getHotelId();
IHotelDao hotelDao = BeanManager.getLazyBeanByClass(HotelDao.class);
Hotel hotel = getHotelEntityByHotelId(hotelId);
if (hotel == null) {
throw new RequestException(
StatusCodeEnume.HOTELIER_NOT_HAS_HOTEL.getMessage(),
StatusCodeEnume.HOTELIER_NOT_HAS_HOTEL.getCode()
);
}

Hotelier currentHotelier = hotelierBlo.getHotelierInfoByUserId(hotelierId);
String hotelId = currentHotelier.getHotelId();
String currentOwnedHotelId = currentHotelier.getHotelId();
if (StringUtils.isEmpty(currentOwnedHotelId)) {
throw new RequestException(
StatusCodeEnume.HOTELIER_NOT_HAS_HOTEL.getMessage(),
StatusCodeEnume.HOTELIER_NOT_HAS_HOTEL.getCode()
);
}

// update hotel
if (hotelParam.getOnSale() != null) hotel.setOnSale(hotelParam.getOnSale());
if (hotelParam.getAddress() != null) hotel.setAddress(hotelParam.getAddress());
if (hotelParam.getName() != null) hotel.setName(hotelParam.getName());
if (hotelParam.getDescription() != null) hotel.setDescription(hotelParam.getDescription());
if (hotelParam.getPostCode() != null) hotel.setPostCode(hotelParam.getPostCode());

UnitOfWorkHelper.getCurrent().registerDirty(
hotel,
hotelDao,
CacheConstant.ENTITY_HOTEL_KEY_PREFIX + hotelId
);

// update amenity (atom update the associate table)
hotelAmenityBlo.updateAmenityIdsForHotel(hotelParam.getAmenityIds(), hotelId);
}

/**
* Exclusive pessimistic lock soluton
*/
@Override
public void editOwnedHotelWithLock(String hotelierId, UpdateHotelParam hotelParam) {
String hotelId = hotelParam.getHotelId();
try {
if (StringUtils.isEmpty(hotelId)) {
IHotelDao hotelDao = BeanManager.getLazyBeanByClass(HotelDao.class);
Hotel hotel = getHotelEntityByHotelId(hotelId);
if (hotel == null) {
throw new RequestException(
StatusCodeEnume.HOTELIER_NOT_HAS_HOTEL.getMessage(),
StatusCodeEnume.HOTELIER_NOT_HAS_HOTEL.getCode()
);
}
IHotelDao hotelDao = BeanManager.getLazyBeanByClass(HotelDao.class);

Hotelier currentHotelier = hotelierBlo.getHotelierInfoByUserId(hotelierId);
String currentOwnedHotelId = currentHotelier.getHotelId();
if (StringUtils.isEmpty(currentOwnedHotelId)) {
throw new RequestException(
StatusCodeEnume.HOTELIER_NOT_HAS_HOTEL.getMessage(),
StatusCodeEnume.HOTELIER_NOT_HAS_HOTEL.getCode()
);
}

// atom operation
synchronized (this) {
Hotel hotel = getHotelEntityByHotelId(hotelId);

if (hotel == null) {
throw new RequestException(
StatusCodeEnume.HOTELIER_NOT_HAS_HOTEL.getMessage(),
StatusCodeEnume.HOTELIER_NOT_HAS_HOTEL.getCode()
);
}
// update hotel
if (hotelParam.getOnSale() != null) hotel.setOnSale(hotelParam.getOnSale());
if (hotelParam.getAddress() != null) hotel.setAddress(hotelParam.getAddress());
if (hotelParam.getName() != null) hotel.setName(hotelParam.getName());
if (hotelParam.getDescription() != null) hotel.setName(hotelParam.getDescription());
if (hotelParam.getDescription() != null) hotel.setDescription(hotelParam.getDescription());
if (hotelParam.getPostCode() != null) hotel.setPostCode(hotelParam.getPostCode());

UnitOfWorkHelper.getCurrent().registerDirty(
Expand Down Expand Up @@ -136,20 +186,19 @@ public HotelVo getHotelInfoByOwnerHotelierIdWithLock(String hotelierId, String c
);
}

// generate hotel vo
hotelVo = new HotelVo();
// copy properties
BeanUtil.copyProperties(hotel, hotelVo);

// embedded value
// embedded value for hotel domain
Money money = new Money();
money.setCurrency(currencyName);
money.setAmount(CurrencyUtil.convertAUDtoCurrency(currencyName, hotel.getMinPrice()));
hotelVo.setMoney(money);
hotel.setMoney(money);

// list amenities
List<HotelAmenity> amenities = hotelAmenityBlo.getAllAmenitiesByHotelId(hotelId);
hotelVo.setAmenities(amenities);
// amenities field of the hotel is using lazy loading, calling get will fetch the database get the field
List<HotelAmenity> amenities = hotel.getAmenities();

// generate hotel vo based on domain
hotelVo = new HotelVo();
// copy properties
BeanUtil.copyProperties(hotel, hotelVo);

return hotelVo;
}
Expand Down Expand Up @@ -232,72 +281,35 @@ public HotelVo getHotelInfoByOwnerHotelierId(String hotelierId, String currencyN
);
}

// generate hotel vo
hotelVo = new HotelVo();
// copy properties
BeanUtil.copyProperties(hotel, hotelVo);

// embedded value
// embedded value for hotel domain
Money money = new Money();
money.setCurrency(currencyName);
money.setAmount(CurrencyUtil.convertAUDtoCurrency(currencyName, hotel.getMinPrice()));
hotelVo.setMoney(money);
hotel.setMoney(money);

// list amenities
List<HotelAmenity> amenities = hotelAmenityBlo.getAllAmenitiesByHotelId(hotelId);
hotelVo.setAmenities(amenities);
// amenities field of the hotel is using lazy loading, calling get will fetch the database get the field
List<HotelAmenity> amenities = hotel.getAmenities();

// generate hotel vo based on domain
hotelVo = new HotelVo();
// copy properties
BeanUtil.copyProperties(hotel, hotelVo);

return hotelVo;
}

@Override
public void editOwnedHotel(String hotelierId, HotelParam hotelParam) {
Hotelier currentHotelier = hotelierBlo.getHotelierInfoByUserId(hotelierId);
String hotelId = currentHotelier.getHotelId();
if (StringUtils.isEmpty(hotelId)) {
public void editHotelByHotelId(UpdateHotelParam updateHotelParam) {
String hotelId = updateHotelParam.getHotelId();
Hotel hotel = getHotelEntityByHotelId(hotelId);

if (hotel == null) {
throw new RequestException(
StatusCodeEnume.HOTELIER_NOT_HAS_HOTEL.getMessage(),
StatusCodeEnume.HOTELIER_NOT_HAS_HOTEL.getCode()
);
}

IHotelDao hotelDao = BeanManager.getLazyBeanByClass(HotelDao.class);
// atom operation
synchronized (this) {
Hotel hotel = getHotelEntityByHotelId(hotelId);

if (hotel == null) {
throw new RequestException(
StatusCodeEnume.HOTELIER_NOT_HAS_HOTEL.getMessage(),
StatusCodeEnume.HOTELIER_NOT_HAS_HOTEL.getCode()
);
}
// update hotel
if (hotelParam.getOnSale() != null) hotel.setOnSale(hotelParam.getOnSale());
if (hotelParam.getAddress() != null) hotel.setAddress(hotelParam.getAddress());
if (hotelParam.getName() != null) hotel.setName(hotelParam.getName());
if (hotelParam.getDescription() != null) hotel.setName(hotelParam.getDescription());
if (hotelParam.getPostCode() != null) hotel.setPostCode(hotelParam.getPostCode());
// hotelDao.updateOne(hotel);
UnitOfWorkHelper.getCurrent().registerDirty(
hotel,
hotelDao,
CacheConstant.ENTITY_HOTEL_KEY_PREFIX + hotelId
);

// update amenity (atom update the associate table)
hotelAmenityBlo.updateAmenityIdsForHotel(hotelParam.getAmenityIds(), hotelId);

// clean up cache
cache.remove(CacheConstant.VO_HOTEL_KEY_PREFIX + hotelId);
cache.remove(CacheConstant.ENTITY_HOTEL_KEY_PREFIX + hotelId);
}

}

@Override
public void editHotelByHotelId(UpdateHotelParam updateHotelParam) {
String hotelId = updateHotelParam.getHotelId();
if (StringUtils.isEmpty(hotelId)) {
throw new RequestException(
StatusCodeEnume.HOTELIER_NOT_HAS_HOTEL.getMessage(),
Expand All @@ -308,15 +320,6 @@ public void editHotelByHotelId(UpdateHotelParam updateHotelParam) {
IHotelDao hotelDao = BeanManager.getLazyBeanByClass(HotelDao.class);
// atom operation
synchronized (this) {
// update hotel
Hotel hotel = getHotelEntityByHotelId(hotelId);

if (hotel == null) {
throw new RequestException(
StatusCodeEnume.HOTELIER_NOT_HAS_HOTEL.getMessage(),
StatusCodeEnume.HOTELIER_NOT_HAS_HOTEL.getCode()
);
}

if (updateHotelParam.getOnSale() != null) hotel.setOnSale(updateHotelParam.getOnSale());
if (updateHotelParam.getAddress() != null) hotel.setAddress(updateHotelParam.getAddress());
Expand Down Expand Up @@ -1029,6 +1032,56 @@ public List<HotelVo> searchHotelsByPageByPostCode(String currencyName, Integer p
}
}

@Override
public void editOwnedHotelV(String hotelierId, UpdateHotelParam hotelParam) {
String hotelId = hotelParam.getHotelId();
int formVersion = hotelParam.getVersion();
IHotelDao hotelDao = BeanManager.getLazyBeanByClass(HotelDao.class);
Hotel hotel = getHotelEntityByHotelId(hotelId);
if (hotel == null) {
throw new RequestException(
StatusCodeEnume.HOTELIER_NOT_HAS_HOTEL.getMessage(),
StatusCodeEnume.HOTELIER_NOT_HAS_HOTEL.getCode()
);
}
int currentVersion = hotel.getVersion();
if (currentVersion > formVersion) {
throw new ResourceConflictException(
"Rejected: room " + hotelId + " info has been modified by hotelier, please refresh" +
"and check latest room information"
);
} else if (currentVersion < formVersion) {
throw new RequestException(
"requested version is too high for room " + hotelId
);
}

Hotelier currentHotelier = hotelierBlo.getHotelierInfoByUserId(hotelierId);
String currentOwnedHotelId = currentHotelier.getHotelId();
if (StringUtils.isEmpty(currentOwnedHotelId)) {
throw new RequestException(
StatusCodeEnume.HOTELIER_NOT_HAS_HOTEL.getMessage(),
StatusCodeEnume.HOTELIER_NOT_HAS_HOTEL.getCode()
);
}

// update hotel
if (hotelParam.getOnSale() != null) hotel.setOnSale(hotelParam.getOnSale());
if (hotelParam.getAddress() != null) hotel.setAddress(hotelParam.getAddress());
if (hotelParam.getName() != null) hotel.setName(hotelParam.getName());
if (hotelParam.getDescription() != null) hotel.setDescription(hotelParam.getDescription());
if (hotelParam.getPostCode() != null) hotel.setPostCode(hotelParam.getPostCode());

UnitOfWorkHelper.getCurrent().registerDirty(
hotel,
hotelDao,
CacheConstant.ENTITY_HOTEL_KEY_PREFIX + hotelId
);

// update amenity (atom update the associate table)
hotelAmenityBlo.updateAmenityIdsForHotel(hotelParam.getAmenityIds(), hotelId);
}

@Override
public List<HotelVo> getAllHotelsByDate(Integer pageNum, Integer pageSize, Integer order) {
// fetch data from db
Expand Down
Loading

0 comments on commit 7740290

Please sign in to comment.