file controller - lethaiduy/MY-SHOP GitHub Wiki
package trainee.module;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;
import lombok.NoArgsConstructor; import trainee.domain.Fbcompany; import trainee.repository.FbcompanyRepository;
@NoArgsConstructor @RestController public class FbcompanyController { @Autowired private FbcompanyRepository repository;
@RequestMapping(value = "/company_delete/{id}", method = RequestMethod.PUT)
public ResponseEntity<Fbcompany> deleteCompany(@PathVariable(value = "id") String id) {
Fbcompany result = repository.findById(id);
if (result == null) {
return new ResponseEntity<Fbcompany>(HttpStatus.NOT_FOUND);
}
result.setDeleteReserved(1);
Fbcompany deletedModel = repository.save(result);
return new ResponseEntity<>(deletedModel, HttpStatus.OK);
}
@RequestMapping(value = "/company_list/", method = RequestMethod.GET)
public ResponseEntity<List<Fbcompany>> searchListCompany() {
List<Fbcompany> list = repository.findAll();
if (list.isEmpty()) {
return new ResponseEntity<List<Fbcompany>>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<List<Fbcompany>>(list, HttpStatus.OK);
}
@RequestMapping(value = "/company_update/{id}", method = RequestMethod.PUT)
public ResponseEntity<Fbcompany> updateCompany(@PathVariable(value = "id") String id,
@Valid @RequestBody Fbcompany company) {
Fbcompany model = repository.findById(id);
if (model == null) {
return new ResponseEntity<Fbcompany>(HttpStatus.NOT_FOUND);
}
model.updateCompany(company);
Fbcompany update = repository.save(model);
return new ResponseEntity<>(update, HttpStatus.OK);
}
@RequestMapping(value = "/company_add", method = RequestMethod.POST)
public ResponseEntity<Fbcompany> addCompany(@Valid @RequestBody Fbcompany company) {
Fbcompany model = repository.findById(company.getId());
if (model != null) {
return new ResponseEntity<Fbcompany>(HttpStatus.NOT_ACCEPTABLE);
}
return new ResponseEntity<Fbcompany>(repository.save(company), HttpStatus.OK);
}
}