use of models.Article in project ninja by ninjaframework.
the class ArticleDao method getFirstArticleForFrontPage.
@UnitOfWork
public Article getFirstArticleForFrontPage() {
EntityManager entityManager = entitiyManagerProvider.get();
Query q = entityManager.createQuery("SELECT x FROM Article x ORDER BY x.postedAt DESC");
Article article = (Article) q.setMaxResults(1).getSingleResult();
return article;
}
use of models.Article in project ninja by ninjaframework.
the class SetupDao method setup.
@Transactional
public void setup() {
EntityManager entityManager = entityManagerProvider.get();
Query q = entityManager.createQuery("SELECT x FROM User x");
List<User> users = (List<User>) q.getResultList();
if (users.size() == 0) {
// Create a new user and save it
User bob = new User("bob@gmail.com", "secret", "Bob");
entityManager.persist(bob);
// Create a new post
Article bobPost3 = new Article(bob, "My third post", lipsum);
entityManager.persist(bobPost3);
// Create a new post
Article bobPost2 = new Article(bob, "My second post", lipsum);
entityManager.persist(bobPost2);
// Create a new post
Article bobPost1 = new Article(bob, post1Title, post1Content);
entityManager.persist(bobPost1);
entityManager.setFlushMode(FlushModeType.COMMIT);
entityManager.flush();
}
}
use of models.Article in project ninja by ninjaframework.
the class ArticleDao method postArticle.
/**
* Returns false if user cannot be found in database.
*/
@Transactional
public boolean postArticle(String username, ArticleDto articleDto) {
EntityManager entityManager = entitiyManagerProvider.get();
Query query = entityManager.createQuery("SELECT x FROM User x WHERE username = :usernameParam");
User user = (User) query.setParameter("usernameParam", username).getSingleResult();
if (user == null) {
return false;
}
Article article = new Article(user, articleDto.title, articleDto.content);
entityManager.persist(article);
return true;
}
Aggregations