diff --git a/Housewives.Api/Controllers/HousewifeController.cs b/Housewives.Api/Controllers/HousewifeController.cs new file mode 100644 index 0000000..977cdde --- /dev/null +++ b/Housewives.Api/Controllers/HousewifeController.cs @@ -0,0 +1,100 @@ +using Housewives.Api.Entities; +using Housewives.Api.Extensions; +using Housewives.Api.Repositories.Contracts; +using Housewives.Models.Dtos; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace Housewives.Api.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class HousewifeController : ControllerBase + { + private readonly IHousewifeRepository housewifeRepository; + + public HousewifeController(IHousewifeRepository housewifeRepository) + { + this.housewifeRepository = housewifeRepository; + } + + [HttpGet] + public async Task>> GetHousewives() + { + try + { + var housewives = await this.housewifeRepository.GetHousewives(); + var quotes = await this.housewifeRepository.GetQuotes(); + + if (housewives == null || quotes == null) + { + return NotFound(); + } + else + { + var housewifeDtos = housewives.ConvertToDto(); + return Ok(housewifeDtos); + } + } + catch (Exception) + { + + return StatusCode(StatusCodes.Status500InternalServerError, + "Error retrieving data from the database"); + } + } + + [HttpGet] + [Route(nameof(GetQuotes))] + public async Task>> GetQuotes() + { + try + { + var housewives = await this.housewifeRepository.GetHousewives(); + var quotes = await this.housewifeRepository.GetQuotes(); + + if (housewives == null || quotes == null) + { + return NotFound(); + } + else + { + var quoteDtos = quotes.ConvertToDto(); + return Ok(quoteDtos); + } + } + catch (Exception) + { + + return StatusCode(StatusCodes.Status500InternalServerError, + "Error retrieving data from the database"); + } + } + + [HttpGet("GetQuotesByHousewife")] + public async Task>> GetQuotesById(int housewifeId) + { + try + { + var quotes = await this.housewifeRepository.GetQuotesById(housewifeId); + + if (quotes == null) + { + return NotFound(); + } + else + { + var quoteDtos = quotes.ConvertToDto(); + return Ok(quoteDtos); + } + } + catch (Exception) + { + + return StatusCode(StatusCodes.Status500InternalServerError, + "Error retrieving data from the database"); + } + } + + } +} diff --git a/Housewives.Api/Controllers/WeatherForecastController.cs b/Housewives.Api/Controllers/WeatherForecastController.cs deleted file mode 100644 index 10a542d..0000000 --- a/Housewives.Api/Controllers/WeatherForecastController.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Microsoft.AspNetCore.Mvc; - -namespace Housewives.Api.Controllers -{ - [ApiController] - [Route("[controller]")] - public class WeatherForecastController : ControllerBase - { - private static readonly string[] Summaries = new[] - { - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" - }; - - private readonly ILogger _logger; - - public WeatherForecastController(ILogger logger) - { - _logger = logger; - } - - [HttpGet(Name = "GetWeatherForecast")] - public IEnumerable Get() - { - return Enumerable.Range(1, 5).Select(index => new WeatherForecast - { - Date = DateTime.Now.AddDays(index), - TemperatureC = Random.Shared.Next(-20, 55), - Summary = Summaries[Random.Shared.Next(Summaries.Length)] - }) - .ToArray(); - } - } -} \ No newline at end of file diff --git a/Housewives.Api/Data/HousewivesDbContext.cs b/Housewives.Api/Data/HousewivesDbContext.cs index 1cd5a8d..b925ce6 100644 --- a/Housewives.Api/Data/HousewivesDbContext.cs +++ b/Housewives.Api/Data/HousewivesDbContext.cs @@ -41,7 +41,7 @@ namespace Housewives.Api.Data modelBuilder.Entity().HasData(new Housewife { Id = 4, - Name = "Heathere Gay", + Name = "Heather Gay", Series = "The Real Housewives of Salt Lake City", ImageUrl = "https://static.wikia.nocookie.net/realitytv-girl/images/4/42/Heather_Gay.jpg/revision/latest?cb=20210509144203" }); diff --git a/Housewives.Api/Extensions/DtoConversions.cs b/Housewives.Api/Extensions/DtoConversions.cs new file mode 100644 index 0000000..f20298e --- /dev/null +++ b/Housewives.Api/Extensions/DtoConversions.cs @@ -0,0 +1,34 @@ +using Housewives.Api.Entities; +using Housewives.Models.Dtos; + +namespace Housewives.Api.Extensions +{ + public static class DtoConversions + { + public static IEnumerable ConvertToDto(this IEnumerable housewives) + { + return (from housewife in housewives + select new HousewifeDto + { + Id = housewife.Id, + Name = housewife.Name, + Series = housewife.Series, + ImageUrl = housewife.ImageUrl + }).ToList(); + } + public static IEnumerable ConvertToDto(this IEnumerable quotes) + { + return (from quote in quotes + select new QuoteDto + { + Id = quote.Id, + HousewifeId = quote.HousewifeId, + Phrase = quote.Phrase, + Season = quote.Season, + Episode = quote.Episode + }).ToList(); + } + + + } +} diff --git a/Housewives.Api/Housewives.Api.csproj b/Housewives.Api/Housewives.Api.csproj index df4bd2d..ae6afd5 100644 --- a/Housewives.Api/Housewives.Api.csproj +++ b/Housewives.Api/Housewives.Api.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -15,4 +15,8 @@ + + + + diff --git a/Housewives.Api/Migrations/20220916234005_FixedName.Designer.cs b/Housewives.Api/Migrations/20220916234005_FixedName.Designer.cs new file mode 100644 index 0000000..9960f7d --- /dev/null +++ b/Housewives.Api/Migrations/20220916234005_FixedName.Designer.cs @@ -0,0 +1,188 @@ +// +using Housewives.Api.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace Housewives.Api.Migrations +{ + [DbContext(typeof(HousewivesDbContext))] + [Migration("20220916234005_FixedName")] + partial class FixedName + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.8") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); + + modelBuilder.Entity("Housewives.Api.Entities.Housewife", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("ImageUrl") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Series") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Housewives"); + + b.HasData( + new + { + Id = 1, + ImageUrl = "https://i.dailymail.co.uk/1s/2022/06/02/08/58580139-10877237-image-a-10_1654154206127.jpg", + Name = "Chanel Ayan", + Series = "The Real Housewives of Dubai" + }, + new + { + Id = 2, + ImageUrl = "https://imgix.bustle.com/uploads/image/2022/6/1/7cbf222b-98cf-4c0b-9643-483ef07f19b9-nup_196331_01313.JPG?w=800&fit=crop&crop=focalpoint&auto=format%2Ccompress&fp-x=0.5787&fp-y=0.22", + Name = "Sara Al Madani", + Series = "The Real Housewives of Dubai" + }, + new + { + Id = 3, + ImageUrl = "https://www.bravotv.com/sites/bravo/files/media_mpx/thumbnails/bravo-video.nbcuni.com/image/NBCU_Bravo/561/911/161202_3434774_This_Is_How_Dorit_and_Lisa_Vanderpump_Became.jpg", + Name = "Lisa Vanderpump", + Series = "The Real Housewives of Beverly Hills" + }, + new + { + Id = 4, + ImageUrl = "https://static.wikia.nocookie.net/realitytv-girl/images/4/42/Heather_Gay.jpg/revision/latest?cb=20210509144203", + Name = "Heather Gay", + Series = "The Real Housewives of Salt Lake City" + }); + }); + + modelBuilder.Entity("Housewives.Api.Entities.Quote", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Episode") + .HasColumnType("int"); + + b.Property("HousewifeId") + .HasColumnType("int"); + + b.Property("Phrase") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Season") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Quotes"); + + b.HasData( + new + { + Id = 1, + Episode = 9, + HousewifeId = 1, + Phrase = "But I'm going to enjoy my life just like I was born today.", + Season = 1 + }, + new + { + Id = 2, + Episode = 11, + HousewifeId = 1, + Phrase = "Be careful who you surround yourself with. You might pick their fashion sense.", + Season = 1 + }, + new + { + Id = 3, + Episode = 3, + HousewifeId = 2, + Phrase = "You're bleeding on people who did not cut you.", + Season = 1 + }, + new + { + Id = 4, + Episode = 0, + HousewifeId = 3, + Phrase = "I'm passionate about dogs, just not about crazy bitches.", + Season = 6 + }, + new + { + Id = 5, + Episode = 0, + HousewifeId = 3, + Phrase = "Life isn't all diamonds and rose, but it should be.", + Season = 3 + }, + new + { + Id = 6, + Episode = 4, + HousewifeId = 4, + Phrase = "For me, the fairytale is the be seen for exactly who I am, and to still be loved.", + Season = 1 + }); + }); + + modelBuilder.Entity("Housewives.Api.Entities.Tag", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Tags"); + + b.HasData( + new + { + Id = 1, + Name = "Advice" + }, + new + { + Id = 2, + Name = "Funny" + }); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Housewives.Api/Migrations/20220916234005_FixedName.cs b/Housewives.Api/Migrations/20220916234005_FixedName.cs new file mode 100644 index 0000000..933cab6 --- /dev/null +++ b/Housewives.Api/Migrations/20220916234005_FixedName.cs @@ -0,0 +1,29 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Housewives.Api.Migrations +{ + public partial class FixedName : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.UpdateData( + table: "Housewives", + keyColumn: "Id", + keyValue: 4, + column: "Name", + value: "Heather Gay"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.UpdateData( + table: "Housewives", + keyColumn: "Id", + keyValue: 4, + column: "Name", + value: "Heathere Gay"); + } + } +} diff --git a/Housewives.Api/Migrations/HousewivesDbContextModelSnapshot.cs b/Housewives.Api/Migrations/HousewivesDbContextModelSnapshot.cs index e82093f..f3219b3 100644 --- a/Housewives.Api/Migrations/HousewivesDbContextModelSnapshot.cs +++ b/Housewives.Api/Migrations/HousewivesDbContextModelSnapshot.cs @@ -71,7 +71,7 @@ namespace Housewives.Api.Migrations { Id = 4, ImageUrl = "https://static.wikia.nocookie.net/realitytv-girl/images/4/42/Heather_Gay.jpg/revision/latest?cb=20210509144203", - Name = "Heathere Gay", + Name = "Heather Gay", Series = "The Real Housewives of Salt Lake City" }); }); diff --git a/Housewives.Api/Program.cs b/Housewives.Api/Program.cs index cbb5d19..3c07834 100644 --- a/Housewives.Api/Program.cs +++ b/Housewives.Api/Program.cs @@ -1,4 +1,6 @@ using Housewives.Api.Data; +using Housewives.Api.Repositories; +using Housewives.Api.Repositories.Contracts; using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); @@ -15,6 +17,8 @@ builder.Services.AddDbContextPool(options => options.UseSqlServer(builder.Configuration.GetConnectionString("HousewivesConnection")) ); +builder.Services.AddScoped(); + var app = builder.Build(); diff --git a/Housewives.Api/Repositories/Contracts/IHousewifeRepository.cs b/Housewives.Api/Repositories/Contracts/IHousewifeRepository.cs new file mode 100644 index 0000000..5a391b3 --- /dev/null +++ b/Housewives.Api/Repositories/Contracts/IHousewifeRepository.cs @@ -0,0 +1,15 @@ +using Housewives.Api.Entities; + +namespace Housewives.Api.Repositories.Contracts +{ + public interface IHousewifeRepository + { + Task> GetHousewives(); + Task> GetQuotes(); + Task> GetQuotesById(int housewifeId); + + Task GetHousewife(int id); + Task GetQuote(int id); + + } +} diff --git a/Housewives.Api/Repositories/HousewifeRepository.cs b/Housewives.Api/Repositories/HousewifeRepository.cs new file mode 100644 index 0000000..54dde29 --- /dev/null +++ b/Housewives.Api/Repositories/HousewifeRepository.cs @@ -0,0 +1,46 @@ +using Housewives.Api.Data; +using Housewives.Api.Entities; +using Housewives.Api.Repositories.Contracts; +using Microsoft.EntityFrameworkCore; + +namespace Housewives.Api.Repositories +{ + public class HousewifeRepository : IHousewifeRepository + { + private readonly HousewivesDbContext housewivesDbContext; + + public HousewifeRepository(HousewivesDbContext housewivesDbContext) + { + this.housewivesDbContext = housewivesDbContext; + } + public Task GetHousewife(int id) + { + throw new NotImplementedException(); + } + + public async Task> GetHousewives() + { + var housewives = await this.housewivesDbContext.Housewives.ToListAsync(); + return housewives; + } + + public Task GetQuote(int id) + { + throw new NotImplementedException(); + } + + public async Task> GetQuotes() + { + var quotes = await this.housewivesDbContext.Quotes.ToListAsync(); + return quotes; + } + + public async Task> GetQuotesById(int housewifeId) + { + var quotes = await (from quote in housewivesDbContext.Quotes + where quote.HousewifeId == housewifeId + select quote).ToListAsync(); + return quotes; + } + } +} diff --git a/Housewives.Api/WeatherForecast.cs b/Housewives.Api/WeatherForecast.cs deleted file mode 100644 index 9b0e8ca..0000000 --- a/Housewives.Api/WeatherForecast.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace Housewives.Api -{ - public class WeatherForecast - { - public DateTime Date { get; set; } - - public int TemperatureC { get; set; } - - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); - - public string? Summary { get; set; } - } -} \ No newline at end of file diff --git a/Housewives.Models/Dtos/HousewifeDto.cs b/Housewives.Models/Dtos/HousewifeDto.cs new file mode 100644 index 0000000..17e6084 --- /dev/null +++ b/Housewives.Models/Dtos/HousewifeDto.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Housewives.Models.Dtos +{ + public class HousewifeDto + { + public int Id { get; set; } + public string Name { get; set; } + public string Series { get; set; } + public string ImageUrl { get; set; } + } +} diff --git a/Housewives.Models/Dtos/QuoteDto.cs b/Housewives.Models/Dtos/QuoteDto.cs new file mode 100644 index 0000000..a719a44 --- /dev/null +++ b/Housewives.Models/Dtos/QuoteDto.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Housewives.Models.Dtos +{ + public class QuoteDto + { + public int Id { get; set; } + public int HousewifeId { get; set; } + public string Phrase { get; set; } + public int Season { get; set; } + public int Episode { get; set; } + } +} diff --git a/Housewives.Models/Dtos/TagDto.cs b/Housewives.Models/Dtos/TagDto.cs new file mode 100644 index 0000000..09768cf --- /dev/null +++ b/Housewives.Models/Dtos/TagDto.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Housewives.Models.Dtos +{ + public class TagDto + { + public int Id { get; set; } + public string Name { get; set; } + } +} diff --git a/Housewives.Models/Housewives.Models.csproj b/Housewives.Models/Housewives.Models.csproj new file mode 100644 index 0000000..132c02c --- /dev/null +++ b/Housewives.Models/Housewives.Models.csproj @@ -0,0 +1,9 @@ + + + + net6.0 + enable + enable + + + diff --git a/Housewives.Web/Pages/Counter.razor b/Housewives.Web/Pages/Counter.razor deleted file mode 100644 index ef23cb3..0000000 --- a/Housewives.Web/Pages/Counter.razor +++ /dev/null @@ -1,18 +0,0 @@ -@page "/counter" - -Counter - -

Counter

- -

Current count: @currentCount

- - - -@code { - private int currentCount = 0; - - private void IncrementCount() - { - currentCount++; - } -} diff --git a/Housewives.Web/Pages/FetchData.razor b/Housewives.Web/Pages/FetchData.razor deleted file mode 100644 index 7d004a5..0000000 --- a/Housewives.Web/Pages/FetchData.razor +++ /dev/null @@ -1,57 +0,0 @@ -@page "/fetchdata" -@inject HttpClient Http - -Weather forecast - -

Weather forecast

- -

This component demonstrates fetching data from the server.

- -@if (forecasts == null) -{ -

Loading...

-} -else -{ - - - - - - - - - - - @foreach (var forecast in forecasts) - { - - - - - - - } - -
DateTemp. (C)Temp. (F)Summary
@forecast.Date.ToShortDateString()@forecast.TemperatureC@forecast.TemperatureF@forecast.Summary
-} - -@code { - private WeatherForecast[]? forecasts; - - protected override async Task OnInitializedAsync() - { - forecasts = await Http.GetFromJsonAsync("sample-data/weather.json"); - } - - public class WeatherForecast - { - public DateTime Date { get; set; } - - public int TemperatureC { get; set; } - - public string? Summary { get; set; } - - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); - } -} diff --git a/Housewives.Web/Shared/NavMenu.razor b/Housewives.Web/Shared/NavMenu.razor index 733299a..45bb7e5 100644 --- a/Housewives.Web/Shared/NavMenu.razor +++ b/Housewives.Web/Shared/NavMenu.razor @@ -14,16 +14,6 @@ Home - - diff --git a/Housewives.Web/Shared/SurveyPrompt.razor b/Housewives.Web/Shared/SurveyPrompt.razor deleted file mode 100644 index 962027f..0000000 --- a/Housewives.Web/Shared/SurveyPrompt.razor +++ /dev/null @@ -1,16 +0,0 @@ -
- - @Title - - - Please take our - brief survey - - and tell us what you think. -
- -@code { - // Demonstrates how a parent component can supply parameters - [Parameter] - public string? Title { get; set; } -} diff --git a/HousewivesSolution.sln b/HousewivesSolution.sln index 6c11aa4..04318da 100644 --- a/HousewivesSolution.sln +++ b/HousewivesSolution.sln @@ -3,9 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.3.32811.315 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Housewives.Web", "Housewives.Web\Housewives.Web.csproj", "{F4ACEA70-1731-4A3B-8BB9-EF2F4F103C39}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Housewives.Web", "Housewives.Web\Housewives.Web.csproj", "{F4ACEA70-1731-4A3B-8BB9-EF2F4F103C39}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Housewives.Api", "Housewives.Api\Housewives.Api.csproj", "{F70C0E37-A51E-417F-A020-1820C987B2D1}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Housewives.Api", "Housewives.Api\Housewives.Api.csproj", "{F70C0E37-A51E-417F-A020-1820C987B2D1}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Housewives.Models", "Housewives.Models\Housewives.Models.csproj", "{18D146FE-C32C-476C-A06C-6EA693382675}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -21,6 +23,10 @@ Global {F70C0E37-A51E-417F-A020-1820C987B2D1}.Debug|Any CPU.Build.0 = Debug|Any CPU {F70C0E37-A51E-417F-A020-1820C987B2D1}.Release|Any CPU.ActiveCfg = Release|Any CPU {F70C0E37-A51E-417F-A020-1820C987B2D1}.Release|Any CPU.Build.0 = Release|Any CPU + {18D146FE-C32C-476C-A06C-6EA693382675}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {18D146FE-C32C-476C-A06C-6EA693382675}.Debug|Any CPU.Build.0 = Debug|Any CPU + {18D146FE-C32C-476C-A06C-6EA693382675}.Release|Any CPU.ActiveCfg = Release|Any CPU + {18D146FE-C32C-476C-A06C-6EA693382675}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE