Getting Housewives, Quotes, and Quotes from specific Housewives!

This commit is contained in:
Kira 2022-09-16 17:08:41 -07:00
parent 70f2b7a234
commit f6d7f90287
21 changed files with 487 additions and 152 deletions

View File

@ -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<ActionResult<IEnumerable<HousewifeDto>>> 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<ActionResult<IEnumerable<QuoteDto>>> 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<ActionResult<IEnumerable<QuoteDto>>> 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");
}
}
}
}

View File

@ -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<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> 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();
}
}
}

View File

@ -41,7 +41,7 @@ namespace Housewives.Api.Data
modelBuilder.Entity<Housewife>().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"
});

View File

@ -0,0 +1,34 @@
using Housewives.Api.Entities;
using Housewives.Models.Dtos;
namespace Housewives.Api.Extensions
{
public static class DtoConversions
{
public static IEnumerable<HousewifeDto> ConvertToDto(this IEnumerable<Housewife> 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<QuoteDto> ConvertToDto(this IEnumerable<Quote> 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();
}
}
}

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
@ -15,4 +15,8 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Housewives.Models\Housewives.Models.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,188 @@
// <auto-generated />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<string>("ImageUrl")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<int>("Episode")
.HasColumnType("int");
b.Property<int>("HousewifeId")
.HasColumnType("int");
b.Property<string>("Phrase")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<string>("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
}
}
}

View File

@ -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");
}
}
}

View File

@ -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"
});
});

View File

@ -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<HousewivesDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("HousewivesConnection"))
);
builder.Services.AddScoped<IHousewifeRepository, HousewifeRepository>();
var app = builder.Build();

View File

@ -0,0 +1,15 @@
using Housewives.Api.Entities;
namespace Housewives.Api.Repositories.Contracts
{
public interface IHousewifeRepository
{
Task<IEnumerable<Housewife>> GetHousewives();
Task<IEnumerable<Quote>> GetQuotes();
Task<IEnumerable<Quote>> GetQuotesById(int housewifeId);
Task<Housewife> GetHousewife(int id);
Task<Quote> GetQuote(int id);
}
}

View File

@ -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<Housewife> GetHousewife(int id)
{
throw new NotImplementedException();
}
public async Task<IEnumerable<Housewife>> GetHousewives()
{
var housewives = await this.housewivesDbContext.Housewives.ToListAsync();
return housewives;
}
public Task<Quote> GetQuote(int id)
{
throw new NotImplementedException();
}
public async Task<IEnumerable<Quote>> GetQuotes()
{
var quotes = await this.housewivesDbContext.Quotes.ToListAsync();
return quotes;
}
public async Task<IEnumerable<Quote>> GetQuotesById(int housewifeId)
{
var quotes = await (from quote in housewivesDbContext.Quotes
where quote.HousewifeId == housewifeId
select quote).ToListAsync();
return quotes;
}
}
}

View File

@ -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; }
}
}

View File

@ -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; }
}
}

View File

@ -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; }
}
}

View File

@ -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; }
}
}

View File

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@ -1,18 +0,0 @@
@page "/counter"
<PageTitle>Counter</PageTitle>
<h1>Counter</h1>
<p role="status">Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}

View File

@ -1,57 +0,0 @@
@page "/fetchdata"
@inject HttpClient Http
<PageTitle>Weather forecast</PageTitle>
<h1>Weather forecast</h1>
<p>This component demonstrates fetching data from the server.</p>
@if (forecasts == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
@foreach (var forecast in forecasts)
{
<tr>
<td>@forecast.Date.ToShortDateString()</td>
<td>@forecast.TemperatureC</td>
<td>@forecast.TemperatureF</td>
<td>@forecast.Summary</td>
</tr>
}
</tbody>
</table>
}
@code {
private WeatherForecast[]? forecasts;
protected override async Task OnInitializedAsync()
{
forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("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);
}
}

View File

@ -14,16 +14,6 @@
<span class="oi oi-home" aria-hidden="true"></span> Home
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="counter">
<span class="oi oi-plus" aria-hidden="true"></span> Counter
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="fetchdata">
<span class="oi oi-list-rich" aria-hidden="true"></span> Fetch data
</NavLink>
</div>
</nav>
</div>

View File

@ -1,16 +0,0 @@
<div class="alert alert-secondary mt-4">
<span class="oi oi-pencil me-2" aria-hidden="true"></span>
<strong>@Title</strong>
<span class="text-nowrap">
Please take our
<a target="_blank" class="font-weight-bold link-dark" href="https://go.microsoft.com/fwlink/?linkid=2148851">brief survey</a>
</span>
and tell us what you think.
</div>
@code {
// Demonstrates how a parent component can supply parameters
[Parameter]
public string? Title { get; set; }
}

View File

@ -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