Skip to content

Commit

Permalink
Upgrading an application from ASP.NET Core 2.2 to 8.0 (#8)
Browse files Browse the repository at this point in the history
* Update UI elements styles in Index.cshtml

In the PhoneBook Index view, changes have been made in the style of the badge and buttons to partially fix their appearance. This included modifying the background color for the badge and varying the width and icon dimensions of the Edit, Details, and Delete buttons.

* Simplify layout by removing environment-specific CSS and JS

The commit removes the distinction between development and other environments for CSS and Javascript in the _Layout.cshtml file. It simplifies the page layout by consistently using local versions of Bootstrap and Font-Awesome, and by modifying the placement of certain elements. All environments will now use the same CSS and JS, which streamlines testing and deployment.

* Add check for non-empty model in Index view

This commit includes a check to ensure that the list provided to the Index view of the PhoneBook is not empty. Previously, if the list was empty, the view would attempt to take the first entry and fail. The if statement ensures this error is avoided.

* Refactor search functionality in PhoneBookController

Search string validation was simplified and applied directly to the database query rather than to an in-memory list of entries. Further, the ordering and pagination of entries were also shifted to the query level, improving efficiency and performance.

* Refactor database connection setup and enhance options configuration

Removed excessive use of `MySqlConnector` and refactored the way connection strings are retrieved. Also applied cleaner structuring for adding database context services. Added configuration for `DefaultUserOptions` and conditional addition of `DatabaseDeveloperPageExceptionFilter` based on the environment.
  • Loading branch information
shashinma authored Jan 23, 2024
1 parent 1222655 commit faaeca0
Show file tree
Hide file tree
Showing 99 changed files with 57,423 additions and 17,749 deletions.
410 changes: 410 additions & 0 deletions .dockerignore

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Docker Image CI

on:
push:
branches:
- 'master'
- 'dev'
pull_request:
branches: [ "master" ]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Build the PhoneEdit Docker image
working-directory: .
run: docker build . --file Dockerfile --tag my-image-name:$(date +%s)
65 changes: 65 additions & 0 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: Docker Packages

on:
release:
types: [published]

env:
REGISTRY: ${{ vars.REGISTRY }}

jobs:
Build-PhoneEdit:
environment: PhoneEditApplication

runs-on: ubuntu-latest

permissions:
contents: read
packages: write
id-token: write

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Install cosign
if: github.event_name != 'pull_request'
uses: sigstore/cosign-installer@6e04d228eb30da1757ee4e1dd75a0ec73a653e06 #v3.1.1
with:
cosign-release: 'v2.1.1'

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0

- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GH_TOKEN }}

- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0
with:
images: ${{ env.REGISTRY }}/${{ github.actor }}/${{ vars.IMAGE_NAME }}

- name: Build and push PhoneEdit Docker image
id: build-and-push
uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Sign the published PhoneEdit Docker image
if: ${{ github.event_name != 'pull_request' }}
env:
context: .
TAGS: ${{ steps.meta.outputs.tags }}
DIGEST: ${{ steps.build-and-push.outputs.digest }}
run: echo "${TAGS}" | xargs -I {} cosign sign --yes {}@${DIGEST}
29 changes: 29 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This workflow will build a .NET project

name: .NET

on:
push:
branches:
- 'master'
- 'dev'
pull_request:
branches: [ "master" ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,8 @@ paket-files/
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
/appsettings.json
appsettings.json
docker-compose.yml
*.db
*.db*
.DS_Store
21 changes: 0 additions & 21 deletions Areas/Identity/IdentityHostingStartup.cs

This file was deleted.

1 change: 1 addition & 0 deletions Areas/Identity/Pages/Account/Login.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
</label>
</div>
</div>
<br/>
<div class="form-group">
<button type="submit" class="btn btn-primary">Войти</button>
</div>
Expand Down
2 changes: 1 addition & 1 deletion Areas/Identity/Pages/Account/Manage/_Layout.cshtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@{
Layout = "/Areas/Identity/Pages/_Layout.cshtml";
Layout = "_Layout";
}

<h1>Manage your account</h1>
Expand Down
41 changes: 18 additions & 23 deletions Controllers/PhoneBookController.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization.Infrastructure;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using PhoneEdit.Data;
using PhoneEdit.Models;
Expand All @@ -23,9 +17,8 @@ public PhoneBookController(PhonebookContext context)
_context = context;
}

// GET: PhoneBook
[AllowAnonymous]
public async Task<IActionResult> Index(string currentFilter, string searchString, int? cPage)
public async Task<IActionResult> Index(string searchString, string currentFilter, int? cPage)
{
if (searchString != null)
{
Expand All @@ -38,20 +31,26 @@ public async Task<IActionResult> Index(string currentFilter, string searchString

ViewBag.CurrentFilter = searchString;

var entries = from e in _context.Entries
select e;


IQueryable<BookEntry> entriesQuery = _context.Entries;

if (!string.IsNullOrEmpty(searchString))
{
entries = entries.Where(e => e.ToString().Contains(searchString, StringComparison.OrdinalIgnoreCase));
entriesQuery = entriesQuery.Where(e =>
e.Name.Contains(searchString) ||
e.Mail.Contains(searchString) ||
e.Room.Contains(searchString) ||
e.LocalPhoneNumber.Contains(searchString) ||
e.PersonnelNumber.Contains(searchString));
}

entries = entries.OrderBy(e => e.Name);
entriesQuery = entriesQuery.OrderBy(e => e.Name);

int pageSize = 25;
const int pageSize = 25;
int pageNumber = (cPage ?? 1);
return View((await entries.ToListAsync()).ToPagedList(pageNumber,pageSize));

var entries = await entriesQuery.ToPagedListAsync(pageNumber, pageSize);

return View(entries);
}

// GET: PhoneBook/Details/5
Expand Down Expand Up @@ -114,12 +113,9 @@ public async Task<IActionResult> Edit(int? id)
return View(bookEntry);
}

// POST: PhoneBook/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,PersonnelNumber,Name,Position,Department,LocalPhoneNumber,CityPhoneNumber,Mail,Room, Status")] BookEntry bookEntry)
public async Task<IActionResult> Edit(int id, [Bind("Id,PersonnelNumber,Name,Position,Department,LocalPhoneNumber,CityPhoneNumber,Mail,Room")] BookEntry bookEntry)
{
if (id != bookEntry.Id)
{
Expand Down Expand Up @@ -152,7 +148,7 @@ public async Task<IActionResult> Edit(int id, [Bind("Id,PersonnelNumber,Name,Pos
}
return View(bookEntry);
}

// GET: PhoneBook/Delete/5
public async Task<IActionResult> Delete(int? id)
{
Expand Down Expand Up @@ -186,8 +182,7 @@ private bool BookEntryExists(int id)
{
return _context.Entries.Any(e => e.Id == id);
}



// Valid only if personnelNumber is unique
private bool VerifyPersonnelNumber(string personnelNumber, int id)
{
Expand Down
16 changes: 6 additions & 10 deletions Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace PhoneEdit.Data
namespace PhoneEdit.Data;

public class ApplicationDbContext : IdentityDbContext
{
public class ApplicationDbContext : IdentityDbContext
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
}
Loading

0 comments on commit faaeca0

Please sign in to comment.