Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions HabitLoggerMvc.theoxenos/HabitLoggerMvc.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HabitLoggerMvc", "HabitLoggerMvc\HabitLoggerMvc.csproj", "{6A4003D5-D2E2-43A6-94DC-D603369E58CD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6A4003D5-D2E2-43A6-94DC-D603369E58CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6A4003D5-D2E2-43A6-94DC-D603369E58CD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6A4003D5-D2E2-43A6-94DC-D603369E58CD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6A4003D5-D2E2-43A6-94DC-D603369E58CD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
180 changes: 180 additions & 0 deletions HabitLoggerMvc.theoxenos/HabitLoggerMvc/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
# editorconfig.org

# top-most EditorConfig file
root = true

# Default settings:
# A newline ending every file
# Use 4 spaces as indentation
[*]
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

# Specify UTF-8 without byte-order mark
[*.{csproj,locproj,nativeproj,proj,resx,slnx,vbproj}]
charset = utf-8

# Generated code

# C# files
[*.cs]
# New line preferences
csharp_new_line_before_open_brace = all
csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_object_initializers = true
csharp_new_line_before_members_in_anonymous_types = true
csharp_new_line_between_query_expression_clauses = true

# Indentation preferences
csharp_indent_block_contents = true
csharp_indent_braces = false
csharp_indent_case_contents = true
csharp_indent_case_contents_when_block = false
csharp_indent_switch_labels = true
csharp_indent_labels = one_less_than_current

# Modifier preferences
csharp_preferred_modifier_order = public, private, protected, internal, file, static, extern, new, virtual, abstract, sealed, override, readonly, unsafe, required, volatile, async:suggestion

# avoid this. unless absolutely necessary
dotnet_style_qualification_for_field = false:suggestion
dotnet_style_qualification_for_property = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_event = false:suggestion

# Types: use keywords instead of BCL types, and permit var only when the type is clear
csharp_style_var_for_built_in_types = false:suggestion
csharp_style_var_when_type_is_apparent = false:none
csharp_style_var_elsewhere = false:suggestion
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
dotnet_style_predefined_type_for_member_access = true:suggestion

# name all constant fields using PascalCase
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
dotnet_naming_symbols.constant_fields.applicable_kinds = field
dotnet_naming_symbols.constant_fields.required_modifiers = const
dotnet_naming_style.pascal_case_style.capitalization = pascal_case

# static fields should have s_ prefix
dotnet_naming_rule.static_fields_should_have_prefix.severity = suggestion
dotnet_naming_rule.static_fields_should_have_prefix.symbols = static_fields
dotnet_naming_rule.static_fields_should_have_prefix.style = static_prefix_style
dotnet_naming_symbols.static_fields.applicable_kinds = field
dotnet_naming_symbols.static_fields.required_modifiers = static
dotnet_naming_symbols.static_fields.applicable_accessibilities = private, internal, private_protected
dotnet_naming_style.static_prefix_style.required_prefix = s_
dotnet_naming_style.static_prefix_style.capitalization = camel_case

# internal and private fields should be _camelCase
dotnet_naming_rule.camel_case_for_private_internal_fields.severity = suggestion
dotnet_naming_rule.camel_case_for_private_internal_fields.symbols = private_internal_fields
dotnet_naming_rule.camel_case_for_private_internal_fields.style = camel_case_underscore_style
dotnet_naming_symbols.private_internal_fields.applicable_kinds = field
dotnet_naming_symbols.private_internal_fields.applicable_accessibilities = private, internal
dotnet_naming_style.camel_case_underscore_style.required_prefix = _
dotnet_naming_style.camel_case_underscore_style.capitalization = camel_case

# Code style defaults
csharp_using_directive_placement = outside_namespace:suggestion
dotnet_sort_system_directives_first = true
csharp_prefer_braces = true:silent
csharp_preserve_single_line_blocks = true:none
csharp_preserve_single_line_statements = false:none
csharp_prefer_static_local_function = true:suggestion
csharp_prefer_simple_using_statement = false:none
csharp_style_prefer_switch_expression = true:suggestion
dotnet_style_readonly_field = true:suggestion

# Expression-level preferences
dotnet_style_object_initializer = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_prefer_collection_expression = when_types_exactly_match
dotnet_style_explicit_tuple_names = true:suggestion
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_null_propagation = true
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_auto_properties = true:suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true:silent
dotnet_style_prefer_conditional_expression_over_return = true:silent
csharp_prefer_simple_default_expression = true:suggestion

# Expression-bodied members
csharp_style_expression_bodied_methods = true:silent
csharp_style_expression_bodied_constructors = true:silent
csharp_style_expression_bodied_operators = true:silent
csharp_style_expression_bodied_properties = true:silent
csharp_style_expression_bodied_indexers = true:silent
csharp_style_expression_bodied_accessors = true:silent
csharp_style_expression_bodied_lambdas = true:silent
csharp_style_expression_bodied_local_functions = true:silent

# Pattern matching
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
csharp_style_inlined_variable_declaration = true:suggestion

# Null checking preferences
csharp_style_throw_expression = true:suggestion
csharp_style_conditional_delegate_call = true:suggestion

# Other features
csharp_style_prefer_index_operator = false:none
csharp_style_prefer_range_operator = false:none
csharp_style_pattern_local_over_anonymous_function = false:none

# Space preferences
csharp_space_after_cast = false
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_after_comma = true
csharp_space_after_dot = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_after_semicolon_in_for_statement = true
csharp_space_around_binary_operators = before_and_after
csharp_space_around_declaration_statements = do_not_ignore
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_before_comma = false
csharp_space_before_dot = false
csharp_space_before_open_square_brackets = false
csharp_space_before_semicolon_in_for_statement = false
csharp_space_between_empty_square_brackets = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_declaration_name_and_open_parenthesis = false
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_parentheses = false
csharp_space_between_square_brackets = false

# License header
file_header_template = Licensed to the.NET Foundation under one or more agreements.\nThe.NET Foundation licenses this file to you under the MIT license.

# C++ Files

# Xml project files
[*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,nativeproj,locproj}]
indent_size = 2

# Xml build files

# Xml files

# Xml resource files

# Xml config files
[*.{props,targets,config,nuspec}]
indent_size = 2

# Data serialization
[*.{json,yaml,yml}]
indent_size = 2

# Shell scripts
56 changes: 56 additions & 0 deletions HabitLoggerMvc.theoxenos/HabitLoggerMvc/Data/HabitLoggerContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using HabitLoggerMvc.Models;
using Microsoft.EntityFrameworkCore;

namespace HabitLoggerMvc.Data;

public class HabitLoggerContext : DbContext
{
public HabitLoggerContext(DbContextOptions<HabitLoggerContext> options)
: base(options)
{
}

public DbSet<Habit> Habits { get; set; }
public DbSet<HabitUnit> HabitUnits { get; set; }
public DbSet<HabitLog> HabitLogs { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<HabitUnit>().HasIndex(u => u.Name).IsUnique();
modelBuilder.Entity<HabitUnit>(b =>
{
b.HasData
(
new HabitUnit { Id = 1, Name = "Medium glass" },
new HabitUnit { Id = 2, Name = "Small glass" },
new HabitUnit { Id = 3, Name = "Meters" },
new HabitUnit { Id = 4, Name = "Minutes" },
new HabitUnit { Id = 5, Name = "Pages" }
);
});

modelBuilder.Entity<Habit>().HasIndex(h => h.Name).IsUnique();
modelBuilder.Entity<Habit>(b =>
{
b.HasData(
new Habit { Id = 1, Name = "Drinking water", HabitUnitId = 1 },
new Habit { Id = 2, Name = "Drinking fruit sap", HabitUnitId = 2 },
new Habit { Id = 3, Name = "Walking", HabitUnitId = 3 },
new Habit { Id = 4, Name = "Meditation", HabitUnitId = 4 },
new Habit { Id = 5, Name = "Reading", HabitUnitId = 5 }
);
});

modelBuilder.Entity<HabitLog>(b => b.HasData(
new HabitLog { Id = 1, HabitId = 1, Date = new DateTime(2023, 01, 01), Quantity = 8 },
new HabitLog { Id = 2, HabitId = 2, Date = new DateTime(2023, 01, 02), Quantity = 5 },
new HabitLog { Id = 3, HabitId = 3, Date = new DateTime(2023, 01, 03), Quantity = 3 },
new HabitLog { Id = 4, HabitId = 1, Date = new DateTime(2023, 01, 04), Quantity = 7 },
new HabitLog { Id = 5, HabitId = 2, Date = new DateTime(2023, 01, 05), Quantity = 4 },
new HabitLog { Id = 6, HabitId = 4, Date = new DateTime(2023, 01, 06), Quantity = 30 },
new HabitLog { Id = 7, HabitId = 5, Date = new DateTime(2023, 01, 07), Quantity = 150 }
));
}
}
22 changes: 22 additions & 0 deletions HabitLoggerMvc.theoxenos/HabitLoggerMvc/HabitLoggerMvc.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>1984c5be-1522-4d22-a17e-b05d1b49cbcf</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.1"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="10.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

</Project>
11 changes: 11 additions & 0 deletions HabitLoggerMvc.theoxenos/HabitLoggerMvc/Models/ErrorPageModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.AspNetCore.Mvc.RazorPages;

namespace HabitLoggerMvc.Models;

public class ErrorPageModel : PageModel
{
public string? ErrorMessage { get; set; }
}
14 changes: 14 additions & 0 deletions HabitLoggerMvc.theoxenos/HabitLoggerMvc/Models/Habit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.ComponentModel.DataAnnotations;
using HabitLoggerMvc.Validators;

namespace HabitLoggerMvc.Models;

public class Habit
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;

[Display(Name = "Habit Unit")]
[IdRequired(0)]
public int HabitUnitId { get; set; }
}
14 changes: 14 additions & 0 deletions HabitLoggerMvc.theoxenos/HabitLoggerMvc/Models/HabitLog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.ComponentModel.DataAnnotations;
using HabitLoggerMvc.Validators;

namespace HabitLoggerMvc.Models;

public class HabitLog
{
public int Id { get; set; }
public int HabitId { get; set; }

[DataType(DataType.Date)] public DateTime Date { get; set; } = DateTime.Today;

[IsPositive] public int Quantity { get; set; }
}
7 changes: 7 additions & 0 deletions HabitLoggerMvc.theoxenos/HabitLoggerMvc/Models/HabitUnit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace HabitLoggerMvc.Models;

public class HabitUnit
{
public int Id { get; set; }
public string Name { get; set; }
}
18 changes: 18 additions & 0 deletions HabitLoggerMvc.theoxenos/HabitLoggerMvc/Pages/DeleteHabit.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@page
@model DeleteHabit

<h2 class="mb-4 text-center">Deleting Habit: @Model.HabitModel.Name</h2>
<div class="row justify-content-center">
<div class="col-lg-5">
<div class="alert alert-warning" role="alert">
Are you sure you want to delete this habit? This will also delete all associated logs.
</div>
<div>
<form method="post" class="d-inline-block">
@Html.HiddenFor(model => model.HabitModel.Id)
<button class="btn btn-danger" type="submit">Delete</button>
</form>
<a class="btn btn-outline-secondary ms-2" asp-page="./Index">Cancel</a>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using HabitLoggerMvc.Models;
using HabitLoggerMvc.Repositories;
using Microsoft.AspNetCore.Mvc;

namespace HabitLoggerMvc.Pages;

public class DeleteHabit(IRepository<Habit> habitRepository) : ErrorPageModel
{
[BindProperty] public Habit HabitModel { get; set; } = null!;

public async Task<IActionResult> OnGetAsync(int id)
{
if (id <= 0)
{
return NotFound();
}

try
{
HabitModel = await habitRepository.GetByIdAsync(id);
return Page();
}
catch (KeyNotFoundException)
{
return NotFound();
}
catch (Exception ex)
{
ErrorMessage = ex.Message;
return Page();
}
}

public async Task<IActionResult> OnPostAsync(int id)
{
try
{
await habitRepository.DeleteAsync(id);
return RedirectToPage("./Index");
}
catch (Exception ex)
{
ErrorMessage = ex.Message;
return Page();
}
}
}
Loading