Skip to content

Commit

Permalink
Fix bug recipeId=null when noti create new recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
duyetpt committed Nov 29, 2015
1 parent 40b4699 commit f12a0bb
Showing 1 changed file with 42 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,52 +15,48 @@
import com.vn.dailycookapp.utils.ErrorCodeConstant;

/**
*
* @author duyetpt
* Check recipe id
* Get recipe by id
* Get owner information
* Do user favour this recipe
* Do user following this owner
*
* @author duyetpt Check recipe id Get recipe by id Get owner information Do
* user favour this recipe Do user following this owner
*/
public class GetRecipeModel extends AbstractModel {
@Override
protected void preExecute(String... data) throws Exception {
try {
myId = data[0];
recipeId = data[1];
} catch (Exception ex) {
throw new InvalidParamException();
}
validateData();
}
@Override
protected DCAResponse execute() throws Exception {
DCAResponse response = new DCAResponse(ErrorCodeConstant.SUCCESSUL.getErrorCode());
Recipe recipe = RecipeDAO.getInstance().get(recipeId);
CompactUserInfo owner = UserCache.getInstance().get(recipe.getOwner());
// check user favorite recipe
recipe.setIsFavorite(myId == null ? false : FavoriteDAO.getInstance().isFavorite(myId, recipeId));
// check userid is following owner of recipe
boolean isFollowingOwner = myId == null ? false : FollowingDAO.getInstance().isFollowing(myId, owner.getUserId());
RecipeResponseData data = new RecipeResponseData(recipe, owner);
data.setIsFollowing(isFollowingOwner);
// Increase view number
RecipeDAO.getInstance().increateView(recipeId);
response.setData(data);
return response;
}
private void validateData() throws InvalidParamException {
if (!ObjectId.isValid(recipeId)) {
throw new InvalidParamException();
}
}

@Override
protected void preExecute(String... data) throws Exception {
try {
myId = data[0];
recipeId = data[1];
} catch (Exception ex) {
throw new InvalidParamException();
}
validateData();

}

@Override
protected DCAResponse execute() throws Exception {
DCAResponse response = new DCAResponse(ErrorCodeConstant.SUCCESSUL.getErrorCode());
Recipe recipe = RecipeDAO.getInstance().get(recipeId);
CompactUserInfo owner = UserCache.getInstance().get(recipe.getOwner());

// check user favorite recipe
recipe.setIsFavorite(myId == null ? false : FavoriteDAO.getInstance().isFavorite(myId, recipeId));
// check userid is following owner of recipe
boolean isFollowingOwner = myId == null ? false : FollowingDAO.getInstance().isFollowing(myId, owner.getUserId());

RecipeResponseData data = new RecipeResponseData(recipe, owner);
data.setIsFollowing(isFollowingOwner);

// Increase view number
RecipeDAO.getInstance().increateView(recipeId);

response.setData(data);
return response;
}

private void validateData() throws InvalidParamException {
if (!ObjectId.isValid(recipeId)) {
throw new InvalidParamException();
}
}
}

0 comments on commit f12a0bb

Please sign in to comment.