"""

.. module:: CRUD
   :synopsis: Restful User-Service CRUD for the JSON-File
.. moduleauthor:: Ecevit Emre Okan <github.com/eecevit-tgm>

"""
import json

users = ''

"""
Lieset die Daten aus dem JSON File heraus
"""
with open('user.json', 'r') as f:

     users = json.load(f)


def reader():
    """
    Returns the User in the file
    :return: the list of the user in the user.json file
    """
    return users

def delete(name):
    """
    Deletes a specific user, first check if exists, then pop it out, then execute writer()
    :param name: the username
    """
    if name in users:
        users.pop(name)
    writer()


def create(name, id, email):
    """
    Adds the user in the users dict and executes writer()
    :param name: username
    :param id: autogenerated id
    :param email: emailaddress
    """
    users.update({name:{'id':id,'username':name,'email':email}})
    writer()

def writer(user):
    """
    Writes the users into the JSON File
    :param user: the users dict
    """
    users = user
    with open('user.json', 'w') as f:
        f.write(json.dumps(users))
