Skip to content

Commit

Permalink
修改预约业务失败应该抛异常,而不是正常返回
Browse files Browse the repository at this point in the history
  • Loading branch information
liyifeng committed Feb 28, 2017
1 parent 0e967c9 commit dd99874
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 4 deletions.
16 changes: 16 additions & 0 deletions src/main/java/com/soecode/lyf/exception/AppointException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.soecode.lyf.exception;

/**
* 预约业务异常
*/
public class AppointException extends RuntimeException {

public AppointException(String message) {
super(message);
}

public AppointException(String message, Throwable cause) {
super(message, cause);
}

}
16 changes: 16 additions & 0 deletions src/main/java/com/soecode/lyf/exception/NoNumberException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.soecode.lyf.exception;

/**
* 库存不足异常
*/
public class NoNumberException extends RuntimeException {

public NoNumberException(String message) {
super(message);
}

public NoNumberException(String message, Throwable cause) {
super(message, cause);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.soecode.lyf.exception;

/**
* 重复预约异常
*/
public class RepeatAppointException extends RuntimeException {

public RepeatAppointException(String message) {
super(message);
}

public RepeatAppointException(String message, Throwable cause) {
super(message, cause);
}

}
13 changes: 10 additions & 3 deletions src/main/java/com/soecode/lyf/service/impl/BookServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import com.soecode.lyf.entity.Appointment;
import com.soecode.lyf.entity.Book;
import com.soecode.lyf.enums.AppointStateEnum;
import com.soecode.lyf.exception.AppointException;
import com.soecode.lyf.exception.NoNumberException;
import com.soecode.lyf.exception.RepeatAppointException;
import com.soecode.lyf.service.BookService;

@Service
Expand Down Expand Up @@ -51,21 +54,25 @@ public AppointExecution appoint(long bookId, long studentId) {
// 减库存
int update = bookDao.reduceNumber(bookId);
if (update <= 0) {// 库存不足
return new AppointExecution(bookId, AppointStateEnum.NO_NUMBER);
throw new NoNumberException("no number");
} else {
// 执行预约操作
int insert = appointmentDao.insertAppointment(bookId, studentId);
if (insert <= 0) {// 重复预约
return new AppointExecution(bookId, AppointStateEnum.REPEAT_APPOINT);
throw new RepeatAppointException("repeat appoint");
} else {// 预约成功
Appointment appointment = appointmentDao.queryByKeyWithBook(bookId, studentId);
return new AppointExecution(bookId, AppointStateEnum.SUCCESS, appointment);
}
}
} catch (NoNumberException e1) {
throw e1;
} catch (RepeatAppointException e2) {
throw e2;
} catch (Exception e) {
logger.error(e.getMessage(), e);
// 所有编译期异常转换为运行期异常
return new AppointExecution(bookId, AppointStateEnum.INNER_ERROR);
throw new AppointException("appoint inner error:" + e.getMessage());
}
}

Expand Down
14 changes: 13 additions & 1 deletion src/main/java/com/soecode/lyf/web/BookController.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import com.soecode.lyf.dto.AppointExecution;
import com.soecode.lyf.dto.Result;
import com.soecode.lyf.entity.Book;
import com.soecode.lyf.enums.AppointStateEnum;
import com.soecode.lyf.exception.NoNumberException;
import com.soecode.lyf.exception.RepeatAppointException;
import com.soecode.lyf.service.BookService;

@Controller
Expand Down Expand Up @@ -56,7 +59,16 @@ private Result<AppointExecution> appoint(@PathVariable("bookId") Long bookId, @P
if (studentId == null || studentId.equals("")) {
return new Result<>(false, "学号不能为空");
}
AppointExecution execution = bookService.appoint(bookId, studentId);
AppointExecution execution = null;
try {
execution = bookService.appoint(bookId, studentId);
} catch (NoNumberException e1) {
execution = new AppointExecution(bookId, AppointStateEnum.NO_NUMBER);
} catch (RepeatAppointException e2) {
execution = new AppointExecution(bookId, AppointStateEnum.REPEAT_APPOINT);
} catch (Exception e) {
execution = new AppointExecution(bookId, AppointStateEnum.INNER_ERROR);
}
return new Result<AppointExecution>(true, execution);
}

Expand Down

0 comments on commit dd99874

Please sign in to comment.