# core


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

We write source code first, and then tests come after. The tests serve
as both a means to confirm that the code works and also serves as
working examples. The first exported function, parsed_date, is an
example of this pattern.

------------------------------------------------------------------------

<a
href="https://github.com/chrphb/chrphb-friends/blob/main/chrphb_friends/core.py#L15"
target="_blank" style="float:right; font-size:smaller">source</a>

### parsed_date

>  parsed_date (s:str)

*Convert `s` to a datetime*

``` python
parsed_date('2pm')
```

``` python
isinstance(date.fromtimestamp(0), date)
```

The Friend dataclass : this is our main business object.

Note that I use [Python
dataclass](https://docs.python.org/3/library/dataclasses.html) to define
this class.

------------------------------------------------------------------------

<a
href="https://github.com/chrphb/chrphb-friends/blob/main/chrphb_friends/core.py#L21"
target="_blank" style="float:right; font-size:smaller">source</a>

### Friend

>  Friend (id:str, family_name:str, first_name:str, email:str, phone:str,
>              linkedin:str, twitter:str)

*Class for keeping track of a friend item.*

``` python
f1 = Friend(3, 'doo', 'john', 'john@doo.com', '0000000000', 'linkedin', 'twitter')
f1
```

``` python
db = Database("sqlite:///:memory:")
```

``` python
friends1 = db.create(Friend, pk='id')
```

``` python
print(db.schema())
```

``` python
friends1.exists()
```

``` python
friends1.insert(f1)
```

``` python
friends1()
```

``` python
friends1(where="family_name = :my", my="doo")
```

Let’s try to remove this object from the database.

``` python
friends1.delete(f1.id)
```

After these tests, let’s build our Repository service class.

------------------------------------------------------------------------

<a
href="https://github.com/chrphb/chrphb-friends/blob/main/chrphb_friends/core.py#L36"
target="_blank" style="float:right; font-size:smaller">source</a>

### Repository

>  Repository (_db_file_path:str)

*Repository Service for the Friend dataclass. The repository
implementation is based on SQLLite with a file storage*

Let’s have an example here.

First, we create the repository.

``` python
! rm friends_v1.db
```

``` python
repo = Repository('friends_v1.db')
repo
```

You need to call the create method.

``` python
repo.add('doo', 'john', 'john@doo.com', '0000000000', 'linkedin', 'twitter')
```

Now, let’s retrieve my friends:

``` python
repo.friends()
```
