aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: 0c0675e3107d3ecd261d48abb6e93f26e426e09c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# IT-kariera Exam Template

A template that could be useful for the final IT-kariera exam in 2021

## Getting started

Outside of having installed and configured ASP.NET and a database, all you need to do is:
- Create an `appsettings.Develoment.json` file in `ExamTemplate/Web` that is a direct copy of `appsettings.json`, but you put in your connection string
- Optional: Change the database the project uses (refer to [Database](#database))

The template is setup to automatically apply migrations and it comes by default with some.

## Structure

This project uses some main layers. Those layers are the projects inside the solution. Their purpose is:
- Common: Mainly constants and things that could be needed everywhere in the app
- Data: Contains the project context, the context (database) models and migrations
- Service: Contains the business logic, does stuff with the database
- Web: the whole front-end - Views, View models and Controllers

In some layers you have `Configurations` (Web and Service). Put your Automapper configurations there.

The `css` files can be found in `ExamTemplate/Web/wwwroot/css/`. The main stuff are put in `styles.css` and that's where you probably should put your own stuff.

## Tools

The template comes with some very useful tools, found inside `ExamTemplate/tools/`. They are all Bash scripts, so to run them:
- Windows users could just use [git bash](https://gitforwindows.org/). Open a window inside the `ExamTemplate/tools/` folder, type in `./tool-name.sh` and then your parameters (so a command should be something like `./rename-project.sh CarShop`).
- Linux and MacOS users could just use their terminal. **But**, beware: the scripts aren't POSIX compatible, so you might have issues trying to run them.

**No matter the OS, test out the scripts before the exam!** Also, **run these scripts ONLY from inside the `tools` folder!** Otherwise, a lot of stuff will break.

The following sections explain the usage if these scripts.

### rename-project

As the title suggest, with this tool you could rename the whole project. It renames all of the files, folders and text inside files (so usings, namespaces, class names, variable names, etc.) to another given name.

Example: renaming the default (`ExamTemplate`) to `CarShop`
```bash
./rename-project.sh CarShop
```

If you've already renamed your project and want to change the name yet again, just give the script two parameters: the first one is the current name and the second - the new name.

Example: renaming the project called `CarShop` to `BookShop`
```bash
./rename-project.sh CarShop BookShop
```

You could rename stuff manually, but this and all other scripts rely on the naming structure that this script provides.

### add-feature-template

This is a scaffolding-like script. With it, you type in some information about the project and your desired feature and it generates most files you'll need in all layers (Data, Service, Web).

The script is meant to give you the initial stuff to get you started. Outside of creating some files and their **preset contents**, the script can't do anything else. Another limitation is references: it can't detect and add `using` statements, so if you use some fancy data type, make sure to add the appropriate `using`!

 **Example**: Let's say you [renamed](#rename-project) your project to `CarShop` and need a `Car` class that has a `Brand`, `Model` and `Year`.
- The first argument is the project name, then the name of your desired feature (should be the same name you would use when creating a database model) and then the different properties in the format `"type Name"`.
```bash
./add-feature-template.sh CarShop Car "string Brand" "string Model" "int Year"
```
The script will create (and edit where specified) the following files:
```
├─ Data
│  ├─ Models
│  │  └─ Car.cs
│  └─ CarShopContext.cs (Only edited; Adds a DbSet<Car>)
├─ Services
│  ├─ Configurations
│  │  └─ ServiceCarMappings.cs
│  ├─ Models
│  │  └─ Car
│  │     ├─ CarServiceModel.cs
│  │     ├─ CreateCarServiceModel.cs
│  │     └─ EditCarServiceModel.cs
│  └─ CarService.cs
├─ Web
│  ├─ Configurations
│  │  └─ ControllerCarMappings.cs
│  ├─ Controllers
│  │  └─ CarControllers.cs
│  └─ Views
│     ├─ Car
│     │  ├─ CarViewModel.cs
│     │  ├─ CreateCarViewModel.cs
│     │  └─ EditCarViewModel.cs
│     └─ _ViewImports.cshtml (Only edited; Adds reference to the Car View Models namespace)
└─ Startup.cs (Only edited; Adds dependency injection configuration for CarService)
```
All of the models have all given properties **and a "Guid Id" property**. Only the Create models don't have the "Guid Id" property.

Controller and Service have **very basic** CRUD of the model. No view links are added, so you'll need to provide them (and to test out if everything work: just edit the link in the browser).

## Notes

There are some configs that I've made that you may want to change. This section explains them.

### Database

The project is made to use [PostgreSQL](https://www.postgresql.org/) by default. Since not everyone would want to use that, here are some steps for other databases. **Side note**: delete the migrations if you want to use another database and add new ones!

Using MySQL:
1. Install the [MySql.Data.EntityFrameworkCore](https://www.nuget.org/packages/MySql.Data.EntityFrameworkCore/) NuGet package in **both** `ExamTemplate/Web` and `ExamTemplate/Data`
2. In the `ConfigureServices` method, inside `ExamTemplate/Web/Startup.cs`, **and** in the `CreateDbContext` method, inside `ExamTemplate/Data/TemplateContextFactory.cs` change `UseNpgsql` to `UseMySQL`
   - You'll also need to add a using for the package in step 1

Using SQL Server (MSSQL):
1. Install the [Microsoft.EntityFrameworkCore.SqlServer](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.SqlServer/) NuGet package in **both** `ExamTemplate/Web` and `ExamTemplate/Data`
2. In the `ConfigureServices` method, inside `ExamTemplate/Web/Startup.cs`, **and** in the `CreateDbContext` method, inside `ExamTemplate/Data/TemplateContextFactory.cs` change `UseNpgsql` to `UseSqlServer`
   - You'll also need to add a using for the package in step 1

### Cloud

The template uses [Cloudinary](https://cloudinary.com/) for source control. The logic for uploading a file is inside `ExamTemplate/Services/CloudinaryService.cs`.

If you need cloud somewhere, just dependency-inject the service and update your `ExamTemplate/Web/appsettings.Development.json` with the values from your own Cloudinary account.

Usage is simple: just call the `UploadFilesToCloud` method, and give it a `List<IFormFile>` (if you only need to upload one file, create a List and inside it put that file). It will return a List with the URLs of all given files.

This does mean that your view models (that require uploading a file) will need an `IFormFile` property. In the HTML, your file upload form should have a `<input type="file">`.

### HTTPS

HTTPS is disabled by default, since certificates could cause issues on some systems.

To revert:
1. Add `app.UseHttpsRedirection();` in the `Configure` method, inside `ExamTemplate/Web/Startup.cs`
2. Add `https://localhost:5001;` in `"applicationUrl"` in the `Web` section, inside `ExamTemplate/Web/Properties/launchSettings.json`.
   - So, line 21 in the `launchSettings.json` file should look like: `"applicationUrl": "https://localhost:5001;http://localhost:5000",`