package com.github.vovas11.courses.command;

import javax.servlet.http.HttpServletRequest;

import com.github.vovas11.courses.dao.*;
/**
 * {@code RegistrationCommand} gets data about the new user from the HTTP request,
 * creates an instance of the {@code User}, writes the fields of the object and
 * sends it to the database via {@code UserDao}
 * 
 * @author vovas11
 * @see com.github.vovas11.courses.dao.DaoFactory
 * @see com.github.vovas11.courses.dao.UserDao
 */
public class RegistrationCommand implements Command {
    /**
     * Gets data about the new user from the HTTP request,
     * creates an instance of the {@code User}, writes the fields of the object and
     * sends it to the DB via {@code UserDao}. Returns the name of the registration page
     * if the user exists or the name of the login page if the user have been registered.
     * 
     * @param   request   HTTP request
     * @return the the name of the page the server returns to the client
     */
    @Override
    public String execute(HttpServletRequest request) {
	// gets parameters form the HTTP request
	String login = request.getParameter("login");
	String password = request.getParameter("password");
	String firstName = request.getParameter("firstname");
	String lastName = request.getParameter("lastname");
	String department = request.getParameter("department");
	
	// gets the link to the DaoFactory and UserDao
	DaoFactory daoFactory = DaoFactory.getInstance();
	UserDao users = daoFactory.getUserDao();
	// creates the new instance of the User and fills in fields
	
	User user = new User();
	user.setLogin(login);
	user.setPassword(password);
	user.setFirstName(firstName);
	user.setLastName(lastName);
	user.setDepartment(department);
	
	// checks if the user exists
	if (users.isLoginExist(user)) {
	    // returns back to the registration page if the user exists
	    return "/registration.html";
	} else {
	    // returns the name of the login page to proceed
	    users.insert(user);
	    return "/login.html";
	}
    }
}
@@ -387,90 +387,28 @@
 see 
-com.github.vovas11.courses.dao.DaoFactory%0D%0A * @see com.github.vovas11.courses.dao.
+DaoFactory%0D%0A * @see 
 User
@@ -1012,18 +1012,21 @@
 st) %7B%0D%0A%09
-//
+%0D%0A%09/*
  gets pa
@@ -1035,18 +1035,18 @@
 meters f
-o
 r
+o
 m the HT
@@ -1055,16 +1055,19 @@
  request
+ */
 %0D%0A%09Strin
@@ -1335,17 +1335,17 @@
 ;%0D%0A%09%0D%0A%09/
-/
+*
  gets th
@@ -1380,16 +1380,19 @@
  UserDao
+ */
 %0D%0A%09DaoFa
@@ -1481,18 +1481,21 @@
 ao();%0D%0A%09
-//
+%0D%0A%09/*
  creates
@@ -1543,19 +1543,19 @@
 n fields
-%0D%0A%09
+ */
 %0D%0A%09User 
@@ -1729,17 +1729,17 @@
 ;%0D%0A%09%0D%0A%09/
-/
+*
  checks 
@@ -1754,57 +1754,41 @@
 ser 
-exists%0D%0A%09if (users.isLoginExist(user)) %7B%0D%0A%09    //
+(field 'login') exists and if yes
  ret
@@ -1820,133 +1820,176 @@
 tion
+%0D%0A%09 *
  page
+,
  if 
-the user exists%0D%0A%09    return %22/registration.html%22;%0D%0A%09%7D else %7B%0D%0A%09    // returns the name of the login page to proceed
+no inserts new user into database and proceeds to the login page*/%0D%0A%09if (users.isLoginExist(user)) %7B%0D%0A%09    return %22/registration.html%22;%0D%0A%09    %0D%0A%09%7D else %7B
 %0D%0A%09 

Add comment into RegistrationCommand.java