Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
brittoglauber committed Jun 16, 2023
0 parents commit c45782c
Show file tree
Hide file tree
Showing 7,669 changed files with 857,468 additions and 0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./venv
Empty file added api/__init__.py
Empty file.
Binary file added api/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
Binary file added api/__pycache__/admin.cpython-311.pyc
Binary file not shown.
Binary file added api/__pycache__/apps.cpython-311.pyc
Binary file not shown.
Binary file added api/__pycache__/models.cpython-311.pyc
Binary file not shown.
Binary file added api/__pycache__/serializers.cpython-311.pyc
Binary file not shown.
Binary file added api/__pycache__/urls.cpython-311.pyc
Binary file not shown.
Binary file added api/__pycache__/urls_employee.cpython-311.pyc
Binary file not shown.
Binary file added api/__pycache__/urls_query.cpython-311.pyc
Binary file not shown.
Binary file added api/__pycache__/views.cpython-311.pyc
Binary file not shown.
3 changes: 3 additions & 0 deletions api/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions api/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class ApiConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'api'
30 changes: 30 additions & 0 deletions api/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 4.2.2 on 2023-06-16 16:53

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Employee',
fields=[
('id', models.AutoField(primary_key=True, serialize=False)),
('name', models.CharField(max_length=255)),
],
),
migrations.CreateModel(
name='Query',
fields=[
('id', models.AutoField(primary_key=True, serialize=False)),
('date', models.DateField()),
('employee', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api.employee')),
],
),
]
18 changes: 18 additions & 0 deletions api/migrations/0002_query_employee_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.2 on 2023-06-16 18:18

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('api', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='query',
name='employee_name',
field=models.CharField(blank=True, max_length=255, null=True),
),
]
18 changes: 18 additions & 0 deletions api/migrations/0003_alter_query_date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.2 on 2023-06-16 19:40

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('api', '0002_query_employee_name'),
]

operations = [
migrations.AlterField(
model_name='query',
name='date',
field=models.DateField(auto_now_add=True),
),
]
Empty file added api/migrations/__init__.py
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added api/migrations/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
18 changes: 18 additions & 0 deletions api/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.db import models

class Employee(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=255)

def __str__(self):
return self.name


class Query(models.Model):
id = models.AutoField(primary_key=True)
date = models.DateField(auto_now_add=True)
employee = models.ForeignKey(Employee, on_delete=models.CASCADE)
employee_name = models.CharField(max_length=255, null=True, blank=True)

def __str__(self):
return f"Query {self.id}"
15 changes: 15 additions & 0 deletions api/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

from django.db.models import fields
from rest_framework import serializers
from .models import Employee, Query


class EmployeeSerializer(serializers.ModelSerializer):
class Meta:
model = Employee
fields = '__all__'

class QuerySerializer(serializers.ModelSerializer):
class Meta:
model = Query
fields = ['id', 'date', 'employee', 'employee_name']
3 changes: 3 additions & 0 deletions api/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
11 changes: 11 additions & 0 deletions api/urls_employee.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.urls import path
from . import views

urlpatterns = [
path('', views.ApiOverview, name='home'),
path('create/', views.add_employee, name='add-employee'),
path('all/', views.view_employee, name='view_employee'),
path('employee/<int:pk>/', views.view_employee_byId, name='view_employee_byId'),
path('update/<int:pk>/', views.update_employee, name='update-employee'),
path('employee/<int:pk>/delete/', views.delete_employee, name='delete-employee'),
]
8 changes: 8 additions & 0 deletions api/urls_query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.urls import path
from . import views

urlpatterns = [
path('create/', views.add_query, name='add-query'),
path('all/', views.view_query, name='view-query'),
path('consult/', views.search_query_by_employee_name, name='query-by-employee')
]
125 changes: 125 additions & 0 deletions api/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
from django.shortcuts import get_object_or_404
from rest_framework.decorators import api_view
from rest_framework.response import Response
from .models import Employee, Query
from .serializers import EmployeeSerializer, QuerySerializer

from rest_framework import serializers
from rest_framework import status

@api_view(['GET'])
def ApiOverview(request):
api_urls = {
'all_items': '/',
'Search by Name': '/?category=category_name',
'Add': '/create',
'Update': '/update/pk',
'Delete': '/item/pk/delete'
}

return Response(api_urls)


@api_view(['POST'])
def add_employee(request):
employee = EmployeeSerializer(data=request.data)

if Employee.objects.filter(**request.data).exists():
raise serializers.ValidationError('This user already exists!')

if employee.is_valid():
employee.save()
return Response(employee.data)
else:
return Response(status=status.HTTP_404_NOT_FOUND)

@api_view(['GET'])
def view_employee(request):
if request.query_params:
employee = Employee.objects.filter(**request.query_param.dict())
else:
employee = Employee.objects.all()

if employee:
serializer = EmployeeSerializer(employee, many=True)
return Response(serializer.data)
else:
return Response(status=status.HTTP_404_NOT_FOUND)

@api_view(['GET'])
def view_employee_byId(request, pk):
try:
employee = Employee.objects.get(pk=pk)
serializer = EmployeeSerializer(employee)
return Response(serializer.data)
except Employee.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)



@api_view(['POST'])
def update_employee(request, pk):
employee = employee = Employee.objects.get(pk=pk)
data = EmployeeSerializer(instance=employee, data=request.data)

if data.is_valid():
data.save()
return Response(data.data)
else:
return Response(status=status.HTTP_404_NOT_FOUND)


@api_view(['DELETE'])
def delete_employee(request, pk):
employee = get_object_or_404(Employee, pk=pk)
employee.delete()
return Response(status=status.HTTP_202_ACCEPTED)


@api_view(['POST'])
def add_query(request):
employee_name = request.data.get('employee_name')

try:
employee = Employee.objects.get(name=employee_name)
except Employee.DoesNotExist:
return Response({'error': 'Employee not found'}, status=status.HTTP_404_NOT_FOUND)

query_data = {
'date': request.data.get('date'),
'employee': employee.id,
'employee_name': employee_name
}

query_serializer = QuerySerializer(data=query_data)

if query_serializer.is_valid():
query_serializer.save()
return Response(query_serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(query_serializer.errors, status=status.HTTP_400_BAD_REQUEST)

@api_view(['GET'])
def view_query(request):
if request.query_params:
query = Query.objects.filter(**request.query_param.dict())
else:
query = Query.objects.all()


if query:
serializer = QuerySerializer(query, many=True)
return Response(serializer.data)
else:
return Response(data='Query not founded!', status=status.HTTP_404_NOT_FOUND)

@api_view(['POST'])
def search_query_by_employee_name(request):
employee_name = request.data.get('employee_name')

if not employee_name:
return Response({'error': 'employee_name parameter is required'}, status=status.HTTP_400_BAD_REQUEST)

queries = Query.objects.filter(employee_name__icontains=employee_name)
serializer = QuerySerializer(queries, many=True)
return Response(serializer.data)
Empty file added crud/__init__.py
Empty file.
Binary file added crud/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
Binary file added crud/__pycache__/settings.cpython-311.pyc
Binary file not shown.
Binary file added crud/__pycache__/urls.cpython-311.pyc
Binary file not shown.
Binary file added crud/__pycache__/wsgi.cpython-311.pyc
Binary file not shown.
16 changes: 16 additions & 0 deletions crud/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for crud project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'crud.settings')

application = get_asgi_application()
Loading

0 comments on commit c45782c

Please sign in to comment.