Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[frontend][backend] diagnose symptoms(code style, wrong design, low coverage...) #17

Open
hadooboo opened this issue Oct 31, 2019 · 3 comments

Comments

@hadooboo
Copy link
Contributor

Let's find symptoms in our codes and discuss how to mitigate those symptoms!

@ByungHeeCha
Copy link
Contributor

In my code (backend for reviews)...

try:
    req_data = json.loads(request.body.decode())
    restaurant_name = req_data['restaurant_name']
    menu_name = req_data['menu_name']
    content = req_data['content']
    #rating = req_data['rating']
except (KeyError, JSONDecodeError) as e:
    return HttpResponseBadRequest(content=str(e))
restaurant = Restaurant.objects.get(name=restaurant_name)
menu = Menu.objects.get(name=menu_name)
new_review = Review.objects.create(
    author=request.user.profile,
    restaurant=restaurant,
    menu=menu,
    content=content,
    #rating=rating,
    )
dict_new_review = {
    'id': new_review.id,
    'author': new_review.author.id,
    'restaurant': new_review.restaurant.id,
    'menu': new_review.menu.id,
    'content': new_review.content,
    #'rating': new_review.rating,
    'date': new_review.date
    }
return JsonResponse(dict_new_review, status=201)

And...

try:
    review = Review.objects.get(id=review_id)
except ObjectDoesNotExist:
    return HttpResponseNotFound()
if request.user.id != review.author.id:
    return HttpResponse(status=403)
try:
    req_data = json.loads(request.body.decode())
    restaurant_name = req_data['restaurant_name']
    menu_name = req_data['menu_name']
    content = req_data['content']
    rating = req_data['rating']
    img = req_data['image']
except (KeyError, JSONDecodeError) as e:
    return HttpResponseBadRequest(content=str(e))
restaurant = Restaurant.objects.get(name=restaurant_name)
menu = Menu.objects.get(name=menu_name)
review.restaurant = restaurant
review.menu = menu
review.content = content
review.rating = rating
review.review_img = img
review.save()
dict_review = {
    'id': review.id,
    'author': review.author.id,
    'restaurant': review.restaurant.id,
    'menu': review.menu.id,
    'content': review.content,
    'rating': review.rating,
    'date': review.date,
    'image': review.review_img.path
}
return JsonResponse(dict_review, status=201)

I think there are many duplicate codes in this I think I can reduce this by extracting method.

@ByungHeeCha
Copy link
Contributor

For exmaple,

  1. Authentication login, user validation(request.user.id != review.author.id:)
if request.user.id != review.author.id:
    return HttpResponse(status=403)
if not request.user.is_authenticated:
        return HttpResponse(status=401)

can be wrote in 1 method. This code is in all views method, so it will sufficiently reduce our code complexity.
2. jsonDecoding
decoding json and sending bad request can be done in 1 method. This also is in all views method, so it will sufficiently reduce our code complexity.

@tho-kn
Copy link
Contributor

tho-kn commented Oct 31, 2019

In AddReview.js,

imageHandler = (data) => {
this.setState({ image: data.content });
}

method is defined but it's used only once as a props for the button

<ImageSelectPreview onChange={(data) => this.imageHandler(data)} />

as it's very short method, using inline method and just using new line for readability would reduce code complexity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants