Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dzhus/project fix #137

Merged
merged 15 commits into from
Jun 1, 2021
Merged
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
2 changes: 1 addition & 1 deletion OutOfSchool/OutOfSchool.DataAccess/Models/Provider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class Provider
[DataType(DataType.PhoneNumber)]
[RegularExpression(
@"([\d]{10})",
ErrorMessage = "Phone number format is incorrect. Example: 380 50-123-45-67")]
ErrorMessage = "Phone number format is incorrect. Example: 0501234567")]
[DisplayFormat(DataFormatString = "{0:+38 XXX-XXX-XX-XX}")]
public string PhoneNumber { get; set; } = string.Empty;

Expand Down
2 changes: 2 additions & 0 deletions OutOfSchool/OutOfSchool.DataAccess/Models/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ public class User : IdentityUser
public string FirstName { get; set; }

public string Role { get; set; }

public bool IsRegistered { get; set; }
}
}
8 changes: 4 additions & 4 deletions OutOfSchool/OutOfSchool.DataAccess/Models/Workshop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class Workshop
[DataType(DataType.PhoneNumber)]
[Required(ErrorMessage = "Phone number is required")]
[RegularExpression(
@"([\d]{9})",
ErrorMessage = "Phone number format is incorrect. Example: 380 50-123-45-67")]
@"([\d]{10})",
ErrorMessage = "Phone number format is incorrect. Example: 0501234567")]
[DisplayFormat(DataFormatString = "{0:+38 XXX-XXX-XX-XX}")]
public string Phone { get; set; } = string.Empty;

Expand All @@ -37,11 +37,11 @@ public class Workshop
public string Instagram { get; set; } = string.Empty;

[Required(ErrorMessage = "Children's min age is required")]
[Range(0, 16, ErrorMessage = "Min age should be a number from 0 to 16")]
[Range(0, 18, ErrorMessage = "Min age should be a number from 0 to 18")]
public int MinAge { get; set; }

[Required(ErrorMessage = "Children's max age is required")]
[Range(0, 16, ErrorMessage = "Max age should be a number from 0 to 16")]
[Range(0, 18, ErrorMessage = "Max age should be a number from 0 to 18")]
public int MaxAge { get; set; }

[Required(ErrorMessage = "Specify how many times per week lessons will be held")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ namespace OutOfSchool.Services.Repository
public interface IProviderRepository : IEntityRepository<Provider>
{
bool Exists(Provider entity);

bool ExistsUserId(string id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ public ProviderRepository(OutOfSchoolDbContext dbContext)
/// <returns>Bool.</returns>
public bool Exists(Provider entity) => db.Providers.Any(x => x.EdrpouIpn == entity.EdrpouIpn || x.Email == entity.Email);

/// <summary>
/// Checks if the user is trying to create second account.
/// </summary>
/// <param name="id">User id.</param>
/// <returns>Bool.</returns>
public bool ExistsUserId(string id) => db.Providers.Any(x => x.UserId == id);

/// <summary>
/// Add new element.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using OutOfSchool.IdentityServer.Enums;
using OutOfSchool.IdentityServer.ViewModels;
using OutOfSchool.Services.Models;

Expand Down Expand Up @@ -136,14 +137,14 @@ public async Task<IActionResult> Register(RegisterViewModel model)
return View(model);
}

if (Request.Form["Provider"].Count == 1)
if (Request.Form[Role.Provider.ToString()].Count == 1)
{
model.Role = "provider";
model.Role = Role.Provider.ToString().ToLower();
}
else
if (Request.Form["Parent"].Count == 1)
if (Request.Form[Role.Parent.ToString()].Count == 1)
{
model.Role = "parent";
model.Role = Role.Parent.ToString().ToLower();
}
else
{
Expand All @@ -160,6 +161,7 @@ public async Task<IActionResult> Register(RegisterViewModel model)
PhoneNumber = model.PhoneNumber,
CreatingTime = DateTime.Now,
Role = model.Role,
IsRegistered = false,
};

var result = await userManager.CreateAsync(user, model.Password);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<string>("FirstName")
.HasColumnType("nvarchar(max)");

b.Property<bool>("IsRegistered")
.HasColumnType("bit");

b.Property<DateTime>("LastLogin")
.HasColumnType("datetime2");

Expand Down
8 changes: 8 additions & 0 deletions OutOfSchool/OutOfSchool.IdentityServer/Enums/Role.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace OutOfSchool.IdentityServer.Enums
{
public enum Role
{
Provider,
Parent,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
<label class="registration_label" asp-for="PhoneNumber">@SharedLocalizer["Phone"]<span class="registration_star">*</span></label>
<div class="registration_phone">
<div class="registration_phone-code">+38</div>
<input class="registration_input" type="text" asp-for="PhoneNumber" id="phone_number">
<input class="registration_input" maxlength="10" type="text" asp-for="PhoneNumber" id="phone_number">
</div>
<div>
<span asp-validation-for="PhoneNumber"></span>
Expand All @@ -95,7 +95,7 @@
<div>
<label class="registration_label" asp-for="Password">@SharedLocalizer["Password"]<span class="registration_star">*</span></label>
<input class="registration_input registration_input-password" type="Password" asp-for="Password" id="password">
<div class="registration_privacy">
<div class="registration_privacy_password">
<img src="/icons/ic_eye.svg" alt="eye" class="registration_eye" id="password_eye">
</div>
</div>
Expand All @@ -112,7 +112,7 @@
<div>
<label class="registration_label" asp-for="ConfirmPassword">@SharedLocalizer["Confirm password"]<span class="registration_star">*</span></label>
<input class="registration_input registration_input-password" type="password" asp-for="ConfirmPassword" id="repeat_password">
<div class="registration_privacy">
<div class="registration_privacy_confirm_password">
<img src="/icons/ic_eye.svg" alt="eye" class="registration_eye" id="confirm_password_eye">
</div>
</div>
Expand Down
30 changes: 16 additions & 14 deletions OutOfSchool/OutOfSchool.IdentityServer/wwwroot/css/site.css
Original file line number Diff line number Diff line change
Expand Up @@ -381,20 +381,22 @@ a {
color: #ffffff;
cursor: pointer;
}
.registration_privacy {
border: none;
outline: none;
background: inherit;
position: absolute;
bottom: 0;
right: 8px;
width: 30px;
height: 30px;
display: flex !important;
align-items: center !important;
justify-content: center !important;
}
.registration_privacy img {
.registration_privacy_password,
.registration_privacy_confirm_password {
border: none;
outline: none;
background: inherit;
position: absolute;
bottom: 0;
right: 8px;
width: 30px;
height: 30px;
display: flex !important;
align-items: center !important;
justify-content: center !important;
}
.registration_privacy_password img,
.registration_privacy_confirm_password img {
height: 16px;
width: auto;
}
Expand Down
24 changes: 21 additions & 3 deletions OutOfSchool/OutOfSchool.IdentityServer/wwwroot/js/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,45 @@ let check_confirmPasswordEye = false;
let password = document.getElementById('password');
let repeatPassword = document.getElementById('repeat_password');

if (sessionStorage.getItem("Button") && sessionStorage.getItem("Role")) {
btn_provider.className = "registration_type";
btn_parent.className = "registration_type";
btn_register.setAttribute("name", sessionStorage.getItem("Role"));
document.getElementById(sessionStorage.getItem("Button")).className = "registration_type registration_type-active";
}

if (password.className.includes('input-validation-error')) {
let elements = document.getElementsByClassName('registration_privacy');
let elements = document.getElementsByClassName('registration_privacy_password');
for (let element of elements) {
element.style.height = "65px";
}
}

if (repeatPassword.className.includes('input-validation-error')) {
let elements = document.getElementsByClassName('registration_privacy_confirm_password');
for (let element of elements) {
element.style.height = "65px";
}
}

if (document.getElementsByClassName('validation-summary-errors').length > 0) {
document.getElementById("user_mail").className += ' input-validation-error';
}

btn_parent.addEventListener('click', function () {
btn_parent.className = "registration_type registration_type-active";
btn_provider.className = "registration_type";
btn_register.setAttribute("name", "Parent");
btn_register.setAttribute("name", "Parent");
sessionStorage.setItem("Role", "Parent");
sessionStorage.setItem("Button", "btn_parent");
})

btn_provider.addEventListener('click', function () {
btn_provider.className = "registration_type registration_type-active";
btn_parent.className = "registration_type";
btn_register.setAttribute("name", "Provider");
btn_register.setAttribute("name", "Provider");
sessionStorage.setItem("Role", "Provider");
sessionStorage.setItem("Button", "btn_provider");
})

passwordEye.addEventListener('click', function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,59 @@ public async Task Create_WhenEntityIsValid_ReturnsCreatedEntity()

[Test]
[Order(2)]
public void Create_WhenUserIdExists_ReturnsArgumentException()
{
// Arrange
var expected = new Provider()
{
FullTitle = "Title",
ShortTitle = "ShortTitle",
Website = "Website",
Facebook = "Facebook",
Email = "[email protected]",
Instagram = "Instagram1",
Description = "Description1",
DirectorBirthDay = new DateTime(1975, month: 10, 5),
EdrpouIpn = "55545000",
PhoneNumber = "1111111111",
Founder = "Founder",
Ownership = OwnershipType.Private,
Type = ProviderType.TOV,
Status = false,
LegalAddressId = 166,
ActualAddressId = 125,
UserId = "de909VV5-5eb7-4b7a-bda8-40a5bfda96a6",
LegalAddress = new Address
{
Id = 166,
Region = "Region166",
District = "District166",
City = "City166",
Street = "Street166",
BuildingNumber = "BuildingNumber166",
Latitude = 0,
Longitude = 0,
},
ActualAddress = new Address
{
Id = 125,
Region = "Region125",
District = "District125",
City = "City125",
Street = "Street125",
BuildingNumber = "BuildingNumber125",
Latitude = 0,
Longitude = 0,
},
};

// Act and Assert
Assert.ThrowsAsync<ArgumentException>(
async () => await service.Create(expected.ToModel()).ConfigureAwait(false));
}

[Test]
[Order(3)]
public void Create_NotUniqueEntity_ReturnsArgumentException()
{
// Arrange
Expand Down Expand Up @@ -175,7 +228,7 @@ public void Create_NotUniqueEntity_ReturnsArgumentException()
}

[Test]
[Order(3)]
[Order(4)]
public async Task GetAll_WhenCalled_ReturnsAllEntities()
{
// Arrange
Expand All @@ -189,7 +242,7 @@ public async Task GetAll_WhenCalled_ReturnsAllEntities()
}

[Test]
[Order(4)]
[Order(5)]
[TestCase(1)]
public async Task GetById_WhenIdIsValid_ReturnsEntity(long id)
{
Expand All @@ -204,7 +257,7 @@ public async Task GetById_WhenIdIsValid_ReturnsEntity(long id)
}

[Test]
[Order(5)]
[Order(6)]
[TestCase(10)]
public void GetById_WhenIdIsInvalid_ThrowsArgumentOutOfRangeException(long id)
{
Expand All @@ -214,7 +267,7 @@ public void GetById_WhenIdIsInvalid_ThrowsArgumentOutOfRangeException(long id)
}

[Test]
[Order(6)]
[Order(7)]
public async Task Update_WhenEntityIsValid_UpdatesExistedEntity()
{
// Arrange
Expand All @@ -232,7 +285,7 @@ public async Task Update_WhenEntityIsValid_UpdatesExistedEntity()
}

[Test]
[Order(7)]
[Order(8)]
public void Update_WhenEntityIsInvalid_ThrowsDbUpdateConcurrencyException()
{
// Arrange
Expand All @@ -247,7 +300,7 @@ public void Update_WhenEntityIsInvalid_ThrowsDbUpdateConcurrencyException()
}

[Test]
[Order(8)]
[Order(9)]
[TestCase(1)]
public async Task Delete_WhenIdIsValid_DeletesEntity(long id)
{
Expand All @@ -263,7 +316,7 @@ public async Task Delete_WhenIdIsValid_DeletesEntity(long id)
}

[Test]
[Order(9)]
[Order(10)]
[TestCase(10)]
public void Delete_WhenIdIsInvalid_ThrowsArgumentNullException(long id)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public async Task<IActionResult> GetById(long id)

return Ok(await service.GetById(id).ConfigureAwait(false));
}

/// <summary>
/// To create new Parent and add to the DB.
/// </summary>
Expand Down
23 changes: 23 additions & 0 deletions OutOfSchool/OutOfSchool.WebApi/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using OutOfSchool.WebApi.Models;
using OutOfSchool.WebApi.Services;

namespace OutOfSchool.WebApi.Controllers
Expand Down Expand Up @@ -59,5 +60,27 @@ public async Task<IActionResult> GetUserById(string id)
return BadRequest(ex.Message);
}
}

/// <summary>
/// Update info about the User.
/// </summary>
/// <param name="dto">Entity to update.</param>
/// <returns>Updated Provider.</returns>
[Authorize(AuthenticationSchemes = "Bearer")]
[Authorize(Roles = "parent,provider,admin")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[HttpPut]
public async Task<IActionResult> Update(UserDto dto)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}

return Ok(await userService.Update(dto).ConfigureAwait(false));
}
}
}
Loading