Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hw002 #176

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

Hw002 #176

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ log
*.patch


/src/main/java/META-INF/MANIFEST.MF
code/
45 changes: 43 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>ru.javawebinar</groupId>
<artifactId>topjava</artifactId>
<packaging>jar</packaging>
<packaging>war</packaging>

<version>1.0-SNAPSHOT</version>

Expand All @@ -15,6 +15,12 @@
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<spring.version>5.2.0.RELEASE</spring.version>

<!-- Logging -->
<logback.version>1.2.3</logback.version>
<slf4j.version>1.7.28</slf4j.version>
</properties>

<build>
Expand All @@ -34,6 +40,41 @@
</build>

<dependencies>
<!-- Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>runtime</scope>
</dependency>

<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>

<!--Web-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>

<profiles>
Expand Down
11 changes: 0 additions & 11 deletions src/main/java/ru/javawebinar/topjava/Main.java

This file was deleted.

34 changes: 34 additions & 0 deletions src/main/java/ru/javawebinar/topjava/SpringMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ru.javawebinar.topjava;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import ru.javawebinar.topjava.model.Role;
import ru.javawebinar.topjava.model.User;
import ru.javawebinar.topjava.to.MealTo;
import ru.javawebinar.topjava.web.meal.MealRestController;
import ru.javawebinar.topjava.web.user.AdminRestController;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Month;
import java.util.Arrays;
import java.util.List;

public class SpringMain {
public static void main(String[] args) {
// java 7 automatic resource management
try (ConfigurableApplicationContext appCtx = new ClassPathXmlApplicationContext("spring/spring-app.xml")) {
System.out.println("Bean definition names: " + Arrays.toString(appCtx.getBeanDefinitionNames()));
AdminRestController adminUserController = appCtx.getBean(AdminRestController.class);
adminUserController.create(new User(null, "userName", "[email protected]", "password", Role.ROLE_ADMIN));
System.out.println();

MealRestController mealController = appCtx.getBean(MealRestController.class);
List<MealTo> filteredMealsWithExcess =
mealController.getBetween(
LocalDate.of(2015, Month.MAY, 30), LocalTime.of(7, 0),
LocalDate.of(2015, Month.MAY, 31), LocalTime.of(11, 0));
filteredMealsWithExcess.forEach(System.out::println);
}
}
}
26 changes: 26 additions & 0 deletions src/main/java/ru/javawebinar/topjava/model/AbstractBaseEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ru.javawebinar.topjava.model;

public abstract class AbstractBaseEntity {
protected Integer id;

protected AbstractBaseEntity(Integer id) {
this.id = id;
}

public void setId(Integer id) {
this.id = id;
}

public Integer getId() {
return id;
}

public boolean isNew() {
return this.id == null;
}

@Override
public String toString() {
return getClass().getSimpleName() + ":" + id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ru.javawebinar.topjava.model;

public abstract class AbstractNamedEntity extends AbstractBaseEntity {

protected String name;

protected AbstractNamedEntity(Integer id, String name) {
super(id);
this.name = name;
}

public void setName(String name) {
this.name = name;
}

public String getName() {
return this.name;
}

@Override
public String toString() {
return super.toString() + '(' + name + ')';
}
}
46 changes: 46 additions & 0 deletions src/main/java/ru/javawebinar/topjava/model/Meal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ru.javawebinar.topjava.model;

import java.time.LocalDateTime;

public class Meal extends AbstractBaseEntity {
private final LocalDateTime dateTime;
private final String description;
private final int calories;

public Meal(LocalDateTime dateTime, String description, int calories) {
this(null, dateTime, description, calories);
}

public Meal(Integer id, LocalDateTime dateTime, String description, int calories) {
super(id);
this.dateTime = dateTime;
this.description = description;
this.calories = calories;
}

public void setId(int id) {
this.id = id;
}

public LocalDateTime getDateTime() {
return dateTime;
}

public String getDescription() {
return description;
}

public int getCalories() {
return calories;
}

@Override
public String toString() {
return "Meal{" +
"id=" + id +
", dateTime=" + dateTime +
", description='" + description + '\'' +
", calories=" + calories +
'}';
}
}
6 changes: 6 additions & 0 deletions src/main/java/ru/javawebinar/topjava/model/Role.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ru.javawebinar.topjava.model;

public enum Role {
ROLE_USER,
ROLE_ADMIN
}
91 changes: 91 additions & 0 deletions src/main/java/ru/javawebinar/topjava/model/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package ru.javawebinar.topjava.model;

import java.util.Date;
import java.util.EnumSet;
import java.util.Set;

import static ru.javawebinar.topjava.util.MealsUtil.DEFAULT_CALORIES_PER_DAY;

public class User extends AbstractNamedEntity {

private String email;

private String password;

private boolean enabled = true;

private Date registered = new Date();

private Set<Role> roles;

private int caloriesPerDay = DEFAULT_CALORIES_PER_DAY;

public User(Integer id, String name, String email, String password, Role role, Role... roles) {
this(id, name, email, password, DEFAULT_CALORIES_PER_DAY, true, EnumSet.of(role, roles));
}

public User(Integer id, String name, String email, String password, int caloriesPerDay, boolean enabled, Set<Role> roles) {
super(id, name);
this.email = email;
this.password = password;
this.caloriesPerDay = caloriesPerDay;
this.enabled = enabled;
this.roles = roles;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public void setPassword(String password) {
this.password = password;
}

public Date getRegistered() {
return registered;
}

public void setRegistered(Date registered) {
this.registered = registered;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public int getCaloriesPerDay() {
return caloriesPerDay;
}

public void setCaloriesPerDay(int caloriesPerDay) {
this.caloriesPerDay = caloriesPerDay;
}

public boolean isEnabled() {
return enabled;
}

public Set<Role> getRoles() {
return roles;
}

public String getPassword() {
return password;
}

@Override
public String toString() {
return "User (" +
"id=" + id +
", email=" + email +
", name=" + name +
", enabled=" + enabled +
", roles=" + roles +
", caloriesPerDay=" + caloriesPerDay +
')';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ru.javawebinar.topjava.repository;

import ru.javawebinar.topjava.model.Meal;

import java.time.LocalDateTime;
import java.util.List;

public interface MealRepository {
// null if updated meal do not belong to userId
Meal save(Meal meal, int userId);

// false if meal do not belong to userId
boolean delete(int id, int userId);

// null if meal do not belong to userId
Meal get(int id, int userId);

// ORDERED dateTime desc
List<Meal> getAll(int userId);

// ORDERED dateTime desc
List<Meal> getBetween(LocalDateTime startDate, LocalDateTime endDate, int userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ru.javawebinar.topjava.repository;

import ru.javawebinar.topjava.model.User;

import java.util.List;

public interface UserRepository {
// null if not found, when updated
User save(User user);

// false if not found
boolean delete(int id);

// null if not found
User get(int id);

// null if not found
User getByEmail(String email);

List<User> getAll();
}
Loading