Skip to content

Commit

Permalink
jy
Browse files Browse the repository at this point in the history
  • Loading branch information
markm-freelancer committed Jul 15, 2013
1 parent c7f2646 commit d4163cf
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Deck extends AbstractEntity {
public String toString() {
return new ToStringCreator(this)
.append("name", name)
.append("description", description)
.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public abstract class AbstractEntityService {

protected String urlfragment(String source) {
Validate.notNull(source);
return source.toLowerCase().replaceAll("[^A-Za-z]", "-");
return source.toLowerCase().replaceAll("[^A-Za-z0-9 ]", "").replaceAll(" ", "-");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

public interface DeckServiceCustom {

public Deck create(String owner, Deck deck);
public void excise(Deck deck);

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ public interface PlayerService extends JpaRepository<MagicPlayer, Long>, PlayerS
@Deprecated //really shouldn't be deleting players
void delete(MagicPlayer p);

MagicPlayer findByName(String owner);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,40 @@

import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;

import com.mtg.commons.models.collections.Deck;
import com.mtg.commons.models.magic.MagicPlayer;
import com.mtg.commons.services.AbstractEntityService;
import com.mtg.commons.services.DeckService;
import com.mtg.commons.services.DeckServiceCustom;
import com.mtg.commons.services.ImageService;
import com.mtg.commons.services.PlayerService;

public class DeckServiceCustomImpl implements DeckServiceCustom {
@Transactional
public class DeckServiceCustomImpl extends AbstractEntityService implements DeckServiceCustom {

@Resource
private DeckService decks;

@Resource
private ImageService images;

@Resource
private PlayerService accounts;

@Override
public Deck create(String owner, Deck deck) {
deck.setUrlFragment(urlfragment(deck.getName()));
Deck saved = decks.save(deck);

MagicPlayer player = accounts.findByName(owner);
saved.setOwner(player);
player.getDecks().add(saved);

return saved;
}

@SuppressWarnings("deprecation")
@Override
public void excise(Deck deck) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.mtg.commons.services;

import org.junit.Test;

public class AbstractEntityServiceTest {

AbstractEntityService service = new AbstractEntityService() {

};

@Test
public void testUrlFragment() {
String name = "M14 midnight prerelease's deck";
System.out.println(service.urlfragment(name));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ public interface AccountDeckController {
@ResponseBody
@RequestMapping(value = "/new", method = RequestMethod.POST)
JSON newdeck(Principal principal, NewDeckForm form, BindingResult binding);

@RequestMapping("/edit/{id}/{urlFragment}")
ModelAndView edit(Principal principal, Long id, String urlFragment);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@

import java.security.Principal;

import javax.annotation.Resource;

import org.apache.commons.lang.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

import com.mtg.commons.models.collections.Deck;
import com.mtg.commons.services.DeckService;
import com.mtg.security.services.AccountService;
import com.mtg.web.controller.AccountDeckController;
import com.mtg.web.controller.GenericController;
import com.mtg.web.dto.JSON;
Expand All @@ -18,6 +25,12 @@ public class AccountDeckControllerImpl extends GenericController implements Acco

private static Logger log = LoggerFactory.getLogger(AccountDeckControllerImpl.class);

@Resource
private DeckService decks;

@Resource
private AccountService accounts;

@Override
public ModelAndView newdeckForm(Principal principal) {

Expand All @@ -28,7 +41,26 @@ public ModelAndView newdeckForm(Principal principal) {

@Override
public JSON newdeck(Principal principal, NewDeckForm form, BindingResult binding) {
return null;

log.info("Deck creation form received. user={}, deck={}", name(principal), form.toDeck());

if(binding.hasErrors()) {
return JSON.error(firstError(binding));
}

Deck deck = decks.create(principal.getName(), form.toDeck());

return JSON.ok(deck.getId() + "/" + deck.getUrlFragment());
}

@Override
public ModelAndView edit(Principal principal, @PathVariable Long id, @PathVariable String urlFragment) {

Deck deck = decks.findOne(id);
Validate.notNull(deck);

return mav("/account/deck/edit")
.addObject("deck", deck);
}

}
9 changes: 9 additions & 0 deletions mtg-web/src/main/java/com/mtg/web/dto/NewDeckForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.hibernate.validator.constraints.NotEmpty;

import com.mtg.commons.models.collections.Deck;

public class NewDeckForm {

@NotEmpty(message = "Deck must have a name!")
Expand All @@ -10,6 +12,13 @@ public class NewDeckForm {
@NotEmpty(message = "Deck must have a description!")
private String description;

public Deck toDeck() {
Deck deck = new Deck();
deck.setName(name);
deck.setDescription(description);
return deck;
}

public String getName() {
return name;
}
Expand Down
2 changes: 1 addition & 1 deletion mtg-web/src/main/webapp/WEB-INF/view/account/dashboard.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
<strong><a href="<@spring.url '/u/${account.player.name }/deck/${deck.urlFragment }' />">${deck.name }</a></strong>
</td>
<td>
<a href="<@spring.url '/account/editdeck/${deck.urlFragment}' />" class="btn btn-success btn-mini"><i class="icon-edit icon-white"></i> Edit</a>
<a href="<@spring.url '/account/deck/edit/${deck.id}/${deck.urlFragment}' />" class="btn btn-success btn-mini"><i class="icon-edit icon-white"></i> Edit</a>
<a href="javascript:;" class="delete-deck btn btn-danger btn-mini" data-deck-name="${deck.name }" data-deck-id="${deck.urlFragment }"><i class="icon-remove icon-white"></i> Delete</a>
</td>
</tr>
Expand Down
1 change: 1 addition & 0 deletions mtg-web/src/main/webapp/WEB-INF/view/account/deck/edit.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h3>Edit ${deck.name }</h3>
2 changes: 1 addition & 1 deletion mtg-web/src/main/webapp/WEB-INF/view/account/deck/new.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

<script>
var newdeckUrls = {
editdeck : '<@spring.url "/account/deck/edit" />'
editdeck : '<@spring.url "/account/deck/edit/" />'
}
$(function(){
Expand Down

0 comments on commit d4163cf

Please sign in to comment.