Commit 57cb03d1 authored by Vincent Horváth's avatar Vincent Horváth

fixed

parent 08c2445e
......@@ -62,7 +62,7 @@ public class EntityOverwriteGenerator extends AngularGenerator {
if (field.getType() == ONE_TO_ONE) {
type = field.getRelationShip().getName();
} else if (field.getType() == ONE_TO_MANY) {
type = field.getRelationShip().getName()+"[]";
type = field.getRelationShip().getName();
} else if (field.getType() == MANY_TO_MANY) {
type = field.getRelationShip().getName()+"[]";
} else {
......
package sk.tuke.bfi.angular.generator.preprocessor;
import org.jsoup.nodes.Element;
import sk.tuke.bfi.angular.generator.AngularPreprocessor;
import sk.tuke.bfi.core.entity.model.Field;
import sk.tuke.bfi.core.entity.model.Model;
import sk.tuke.bfi.core.preprocessor.annotation.Preprocessor;
import static sk.tuke.bfi.core.preprocessor.html.BfiAttribute.BFI_FIELD;
import static sk.tuke.bfi.core.preprocessor.html.BfiAttribute.BFI_MODEL;
@Preprocessor("label")
public class LabelPreprocessor extends AngularPreprocessor {
@Override
public void replace() {
Element element = getElement();
Model model = null;
final String modelName = element.attr(BFI_MODEL.toString());
final String fieldName = element.attr(BFI_FIELD.toString());
try {
model = getModel(modelName);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
final Field field = model.getField(fieldName);
if (field == null) {
throw new RuntimeException("Field " + fieldName + " is not exist in model " + modelName);
}
Element inputElement;
inputElement = getLabel(field, model);
element.replaceWith(inputElement);
}
private Element getLabel(Field field, Model model) {
Element element = getElement();
Element outputElement = new Element("span");
outputElement.append(field.getLabel());
copyAttributes(element, outputElement);
return outputElement;
}
}
package sk.tuke.bfi.symfony.generator.impl.overwrite;
import org.stringtemplate.v4.ST;
import sk.tuke.bfi.core.entity.model.Field;
import sk.tuke.bfi.core.entity.model.FieldType;
import sk.tuke.bfi.core.generator.annotation.Generator;
import sk.tuke.bfi.core.entity.model.Model;
import sk.tuke.bfi.core.generator.annotation.GeneratorMethod;
import sk.tuke.bfi.core.generator.annotation.GeneratorSaveTo;
import sk.tuke.bfi.symfony.generator.SymfonyGenerator;
import java.util.ArrayList;
import java.util.List;
import static sk.tuke.bfi.core.helper.HelpVariable.FILE_EXTENSIONS_SEPARATOR;
@Generator(
......@@ -38,6 +43,20 @@ public class ControllerOverwriteGenerator extends SymfonyGenerator {
template.add("baseRoute", "/api");
template.add("className", model.getName() + getPOSTFIX());
template.add("modelName", model.getName());
String useRelationModels = "";
List<String> relationsName = new ArrayList<>();
for (Field field : model.getFields()) {
if (field.getRelationShip() == null) {
continue;
}
if(relationsName.contains(field.getRelationShip().getName())){
continue;
}
useRelationModels += "use App\\Entity\\"+field.getRelationShip().getName()+";\n";
relationsName.add(field.getRelationShip().getName());
}
template.add("useRelationModels", useRelationModels);
return template;
}
......@@ -62,18 +81,63 @@ public class ControllerOverwriteGenerator extends SymfonyGenerator {
@GeneratorMethod(KEY_METHOD_POST)
public ST prepareMethodPost() {
ST template = templateHelper.getTemplate(KEY_METHOD_POST);
String emptyRelation = "";
String setNewRelation = "";
String updateRelation = "";
final String modelProperty = model.getName().toLowerCase();
for (Field field : model.getFields()) {
if (field.getRelationShip() == null) {
continue;
}
final String methodName = field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1);
if (field.getType() == FieldType.MANY_TO_MANY) {
emptyRelation += " $" + modelProperty + "->set" + methodName + "([]);\n";
setNewRelation += " foreach ($request->get(\"" + field.getName() + "\") as $" + field.getName() + "){\n" +
" $" + modelProperty + "->get" + methodName + "()->add($em->getRepository(" + field.getRelationShip().getName() + "::class)->find($" + field.getName() + "['id']) );\n" +
" }\n";
} else if (field.getType() == FieldType.ONE_TO_MANY) {
updateRelation += " $" + modelProperty + "->set" + methodName + "($em->getRepository(" + field.getRelationShip().getName() + "::class)->find($request->get(\"" + field.getName() + "\")['id']));\n";
}
}
template.add("route", model.getName().toLowerCase());
template.add("modelName", model.getName());
template.add("modelPropertyName", model.getName().toLowerCase());
template.add("emptyRelation", emptyRelation);
template.add("setNewRelation", setNewRelation);
template.add("updateRelation", updateRelation);
return template;
}
@GeneratorMethod(KEY_METHOD_PUT)
public ST prepareMethodPut() {
ST template = templateHelper.getTemplate(KEY_METHOD_PUT);
String emptyRelation = "";
String setNewRelation = "";
String updateRelation = "";
final String modelProperty = model.getName().toLowerCase();
for (Field field : model.getFields()) {
if (field.getRelationShip() == null) {
continue;
}
final String methodName = field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1);
if (field.getType() == FieldType.MANY_TO_MANY) {
emptyRelation += " $" + modelProperty + "->set" + methodName + "([]);\n";
setNewRelation += " foreach ($request->get(\"" + field.getName() + "\") as $" + field.getName() + "){\n" +
" $" + modelProperty + "->get" + methodName + "()->add($em->getRepository(" + field.getRelationShip().getName() + "::class)->find($" + field.getName() + "['id']) );\n" +
" }\n";
} else if (field.getType() == FieldType.ONE_TO_MANY) {
updateRelation += " $" + modelProperty + "->set" + methodName + "($em->getRepository(" + field.getRelationShip().getName() + "::class)->find($request->get(\"" + field.getName() + "\")['id']));\n";
}
}
template.add("route", model.getName().toLowerCase());
template.add("modelName", model.getName());
template.add("modelPropertyName", model.getName().toLowerCase());
template.add("emptyRelation", emptyRelation);
template.add("modelPropertyName", modelProperty);
template.add("setNewRelation", setNewRelation);
template.add("updateRelation", updateRelation);
return template;
}
......
......@@ -99,12 +99,16 @@ public class EntityOverwriteGenerator extends SymfonyGenerator {
methodsTemplate.add("fieldName", field.getName());
if (field.getType().equals(FieldType.ONE_TO_ONE)) {
methodsTemplate.add("fieldType", "\\App\\Entity\\" + field.getRelationShip().getName());
methodsTemplate.add("fieldGetType", "\\App\\Entity\\" + field.getRelationShip().getName());
} else if (field.getType().equals(FieldType.ONE_TO_MANY)) {
methodsTemplate.add("fieldType", " array");
methodsTemplate.add("fieldType", "\\App\\Entity\\" + field.getRelationShip().getName());
methodsTemplate.add("fieldGetType", " array");
} else if (field.getType().equals(FieldType.MANY_TO_MANY)) {
methodsTemplate.add("fieldType", " array");
methodsTemplate.add("fieldGetType", " Collection");
} else {
methodsTemplate.add("fieldType", phpMapperHelper.get(field.getType().toString()));
methodsTemplate.add("fieldGetType", phpMapperHelper.get(field.getType().toString()));
}
if (tmpMethodsTemplate != null) {
......
......@@ -38,6 +38,9 @@ public class FormOverwriteGenerator extends SymfonyGenerator {
ST lastField = null;
ST template = null;
for (Field field : model.getFields()) {
if (field.getRelationShip() != null) {
continue;
}
template = templateHelper.getTemplate(KEY_BUILDER);
if (lastField != null) {
template.add("field", "\n" + lastField.render());
......
/**
* @Rest\Get("/#route#/{id}", name="update_#modelName#")
* @Rest\Put("/#route#/{id}", name="update_#modelName#")
*/
public function update#modelName#Action(Request $request, int $id): ?#modelName#
{
......
......@@ -10,6 +10,7 @@ use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Exception\HttpException;
use App\Entity\\#modelName#;
use App\Form\\#modelName#Type;
#useRelationModels#
abstract class #className# extends FOSRestController
......
......@@ -5,10 +5,18 @@ public function create#modelName#Action(Request $request): ?#modelName#
$form->handleRequest($request);
if ($form->isValid()) {
#emptyRelation#
$em = $this->getDoctrine()->getManager();
$em->persist($#modelPropertyName#);
$em->flush();
$#modelPropertyName# = $em->getRepository(#modelName#::class)->find($#modelPropertyName#->getId());
#setNewRelation#
#updateRelation#
$em->persist($#modelPropertyName#);
$em->flush();
return $#modelPropertyName#;
}
......
public function update#modelName#Action(Request $request, int $id): ?#modelName#
{
$em = $this->getDoctrine()->getManager();
$#modelPropertyName# = $em->getRepository(#modelName#::class)->find($id);
$#modelPropertyName# = $em->getRepository(#modelName#::class)->find($id);
#emptyRelation#
$em->persist($#modelPropertyName#);
$em->flush();
#setNewRelation#
#updateRelation#
$form = $this->createForm(#modelName#Type::class, $#modelPropertyName#, ['method' => 'PUT']);
$form->handleRequest($request);
if ($form->isValid()) {
$em->persist($#modelPropertyName#);
$em->flush();
$em->persist($#modelPropertyName#);
$em->flush();
return $#modelPropertyName#;
return $#modelPropertyName#;
}
throw new HttpException(400, "Invalid data");
}
......@@ -2,6 +2,7 @@
namespace App\Entity\Generate;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
......
......@@ -15,7 +15,7 @@ public function set#methodName#(#fieldType# $#fieldName#): void
*
* @return #fieldType#
*/
public function get#methodName#(): ?#fieldType#
public function get#methodName#(): ?#fieldGetType#
{
return $this->#fieldName#;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment