package ciai.controller; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import ciai.exception.AccountAlreadyExistException; import ciai.model.Account; import ciai.repository.AccountRepository; import ciai.repository.DegreeRepository; import ciai.repository.ProfessorRepository; import ciai.repository.RoleRepository; import ciai.repository.StudentRepository; import ciai.service.AppService; import tools.Response; @Controller @RequestMapping(value = "/account") public class AccountController { @Autowired AccountRepository accountsRep; @Autowired StudentRepository studentsRep; @Autowired ProfessorRepository profRep; @Autowired DegreeRepository degreesRep; @Autowired RoleRepository roleRep; @Autowired AppService service; /** * criar uma nova conta */ @RequestMapping(value = "/create", method = RequestMethod.POST) public @ResponseBody ResponseEntity createAccount(@RequestParam Map requestParams){ String name = requestParams.get("name"); String username = requestParams.get("username"); String password = requestParams.get("password"); String type = requestParams.get("type"); String degree = requestParams.get("degree"); if(name.length() < 0 && username.length() < 0 && password.length() < 0 && type.length() < 0 && degree.length() < 0){ return new ResponseEntity(new Response("Invalid fields"),HttpStatus.INTERNAL_SERVER_ERROR); } Account a = null; try { a = service.createAccount(name,username,password,type,degree); } catch (AccountAlreadyExistException e) { return new ResponseEntity(new Response("Account with that username already exist"),HttpStatus.INTERNAL_SERVER_ERROR); } catch (Exception e) { return new ResponseEntity(new Response("Error creating the account"),HttpStatus.INTERNAL_SERVER_ERROR); } if(a == null) return new ResponseEntity(new Response("Error creating the account"),HttpStatus.INTERNAL_SERVER_ERROR); return new ResponseEntity(new Response("Account created with username: "+a.getUsername()),HttpStatus.OK); } }