0

My question or say my confusion is

Does api design depends on how we present in UI?

I will illustrate my confusion with one of an example. Will the api design be different if i want the form to be in multiple step? I mean in the https://www.franchisebazar.com/franchisor-registration if you see the form for company registration, there are section like company information, company brand and business model which are associated with company model. The submit button is only one that means there will be only one api for posting the company data.

But what if I want the UI be different(multiple steps wizard form instead of one single big form) like company info in the first step and after submitting company personal info, the next step will be brand and then business model at last. For this do i need to design an api separately? One for company personal info, one for brand and one for business model.

up to now my design is following

https://gist.github.com/MilanRgm/132eb6c0ba0cf66e48fa0ca4c17ef732

I will brief it in the list again

1) API design for registering company profile using one single big form

2) API design for registering company profile using multiple step way

I am confused though. Any kind of help from community is highly appreciated.

Here is the code as well which i have come up with

models.py

class Company(models.Model):
    owner = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    name = models.CharField(max_length=150, blank=False, null=False)
    phone_number = models.CharField(max_length=15, blank=False, null=False)


class Brand(models.Model):

    company = models.ForeignKey(Company, related_name='company_brand', on_delete=models.CASCADE)
    name = models.CharField(max_length=150, blank=False, null=False)
    website = models.URLField()
    description = models.TextField(blank=False)


class BusinessModel(models.Model):


    company = models.ForeignKey(Company, related_name='company_business_model', on_delete=models.CASCADE)
    industry = models.ForeignKey(Industry, null=True, related_name='industry', on_delete=models.SET_NULL)
    segments = models.ForeignKey(Segment, on_delete=models.SET_NULL, null=True)
    total_investment = models.CharField(max_length=50, choices=TOTAL_INVESTMENT, default=None)

    def __str__(self):
        return self.company.name

serializers.py

class BrandSerializer(serializers.ModelSerializer):

    class Meta:

        model = Brand
        fields = '__all__'



class BusinessModelSerializer(serializers.ModelSerializer):

    class Meta:

        model = BusinessModel
        fields = '__all__'




class CompanySerializer(serializers.ModelSerializer):

    company_brand = BrandSerializer(many=True)
    company_business_model = BusinessModelSerializer(many=True)

    class Meta:

        model = Company
        fields = '__all__'

views.py

class CompanyView(views.APIView):

    serializer_class = CompanySerializer

    def get(self, request, pk=None, format=None):
        reply = {}
        try:
            companies = Company.objects.all()
            if pk:
                company = companies.get(pk=pk)
                reply['data'] = self.serializer_class(company).data
            reply['data'] = self.serializer_class(companies, many=True).data
        except Company.DoesNotExist:
            reply['data'] = []
        return Response(reply, status.HTTP_200_OK)


    def post(self, request, pk=None, format=None):
        reply = {}
        company={}
        if pk is not None:
            try:
                company = Company.object.get(pk=pk)
            except Company.DoesNotExist:
                return error.RequestedResourceNotFound().as_response()
        serialized_data = self.serializer_class(instance=company, data=request.data, partial=True)
        if serialized_data.is_valid():
            serialized_data.save(owner=request.user)
        else:
            return error.ValidationError(serialized_data.errors).as_response()
        reply['data'] = serialized_data.data
        return Response(reply, status.HTTP_200_OK)

1 Answer 1

3

Design of your API depends on the business case. Sometimes, it depends on GUI also. In your case, it is better to design one API. But at the front end, you can design according to your need. You can design multi-step form, save this at Javascript objects, at the end of the process, you can POST this multi-step form data to your API. Or, you can just design on a big form.

Sign up to request clarification or add additional context in comments.

4 Comments

I want to discuss a bit so the confusion gets away from my head. The use case you depict, i understand now but let's say in the front end i designed multi step way and there are 3 steps. I filled one step and hit the submit button. Now that I am in second step, i accidentally closed the page. Then if i load the page, I have to again fill the same step 1 form and carry on. For this use case, multiple endpoint is preferred, right?
@milan you can update your db on submission of first part.
@milan Yes, in your use case, multiple API endpoint is the ultimate solution. You can keep a flag whether from submission is completed or not for the further use of posted data from the database.
Thanks everyone for your time and sharing your knowledge.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.