diff options
author | 杨宇千 <crupest@outlook.com> | 2019-08-22 15:29:03 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-22 15:29:03 +0800 |
commit | 11f01c56b4ea1dbb09d04258bec89f800c6ee2b6 (patch) | |
tree | af83f8596b4fa78713733c0db6b4b6d1695d0ff0 /Timeline | |
parent | fd95f9abc017575b13a31dd16ac72ef663e984d6 (diff) | |
parent | 96c18fb2e17c94ff04094608c705db087400f510 (diff) | |
download | timeline-11f01c56b4ea1dbb09d04258bec89f800c6ee2b6.tar.gz timeline-11f01c56b4ea1dbb09d04258bec89f800c6ee2b6.tar.bz2 timeline-11f01c56b4ea1dbb09d04258bec89f800c6ee2b6.zip |
Merge pull request #48 from crupest/user-details
Add user details.
Diffstat (limited to 'Timeline')
-rw-r--r-- | Timeline/Controllers/UserDetailController.cs | 96 | ||||
-rw-r--r-- | Timeline/Entities/DatabaseContext.cs | 4 | ||||
-rw-r--r-- | Timeline/Entities/UserDetail.cs | 29 | ||||
-rw-r--r-- | Timeline/Migrations/20190822072156_AddUserDetail.Designer.cs | 136 | ||||
-rw-r--r-- | Timeline/Migrations/20190822072156_AddUserDetail.cs | 47 | ||||
-rw-r--r-- | Timeline/Migrations/DatabaseContextModelSnapshot.cs | 43 | ||||
-rw-r--r-- | Timeline/Models/UserDetail.cs | 43 | ||||
-rw-r--r-- | Timeline/Models/Validation/UserDetailValidator.cs | 116 | ||||
-rw-r--r-- | Timeline/Services/DatabaseExtensions.cs | 30 | ||||
-rw-r--r-- | Timeline/Services/UserAvatarService.cs | 21 | ||||
-rw-r--r-- | Timeline/Services/UserDetailService.cs | 135 | ||||
-rw-r--r-- | Timeline/Startup.cs | 1 |
12 files changed, 682 insertions, 19 deletions
diff --git a/Timeline/Controllers/UserDetailController.cs b/Timeline/Controllers/UserDetailController.cs new file mode 100644 index 00000000..5e1183c1 --- /dev/null +++ b/Timeline/Controllers/UserDetailController.cs @@ -0,0 +1,96 @@ +using Microsoft.AspNetCore.Authorization;
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Logging;
+using System.Threading.Tasks;
+using Timeline.Authenticate;
+using Timeline.Models;
+using Timeline.Models.Http;
+using Timeline.Services;
+
+namespace Timeline.Controllers
+{
+ [Route("users/{username}")]
+ [ProducesErrorResponseType(typeof(CommonResponse))]
+ [ApiController]
+ public class UserDetailController : Controller
+ {
+ public static class ErrorCodes
+ {
+ public const int Get_UserNotExist = -1001;
+
+ public const int Patch_Forbid = -2001;
+ public const int Patch_UserNotExist = -2002;
+
+ public const int GetNickname_UserNotExist = -3001;
+ }
+
+ private readonly ILogger<UserDetailController> _logger;
+ private readonly IUserDetailService _service;
+
+ public UserDetailController(ILogger<UserDetailController> logger, IUserDetailService service)
+ {
+ _logger = logger;
+ _service = service;
+ }
+
+ [HttpGet("nickname")]
+ [UserAuthorize]
+ [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(UserDetail))]
+ [ProducesResponseType(StatusCodes.Status404NotFound)]
+ public async Task<IActionResult> GetNickname([FromRoute] string username)
+ {
+ try
+ {
+ var nickname = await _service.GetUserNickname(username);
+ return Ok(new UserDetail
+ {
+ Nickname = nickname
+ });
+ }
+ catch (UserNotExistException)
+ {
+ return NotFound(new CommonResponse(ErrorCodes.GetNickname_UserNotExist, "The user does not exist."));
+ }
+ }
+
+ [HttpGet("details")]
+ [UserAuthorize]
+ [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(UserDetail))]
+ [ProducesResponseType(StatusCodes.Status404NotFound)]
+ public async Task<IActionResult> Get([FromRoute] string username)
+ {
+ try
+ {
+ var detail = await _service.GetUserDetail(username);
+ return Ok(detail);
+ }
+ catch (UserNotExistException)
+ {
+ return NotFound(new CommonResponse(ErrorCodes.Get_UserNotExist, "The user does not exist."));
+ }
+ }
+
+ [HttpPatch("details")]
+ [Authorize]
+ [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(void))]
+ [ProducesResponseType(StatusCodes.Status400BadRequest)]
+ [ProducesResponseType(StatusCodes.Status403Forbidden)]
+ [ProducesResponseType(StatusCodes.Status404NotFound)]
+ public async Task<IActionResult> Patch([FromRoute] string username, [FromBody] UserDetail detail)
+ {
+ if (!User.IsAdmin() && User.Identity.Name != username)
+ return StatusCode(StatusCodes.Status403Forbidden, new CommonResponse(ErrorCodes.Patch_Forbid, "You can't change other's details unless you are admin."));
+
+ try
+ {
+ await _service.UpdateUserDetail(username, detail);
+ return Ok();
+ }
+ catch (UserNotExistException)
+ {
+ return NotFound(new CommonResponse(ErrorCodes.Patch_UserNotExist, "The user does not exist."));
+ }
+ }
+ }
+}
diff --git a/Timeline/Entities/DatabaseContext.cs b/Timeline/Entities/DatabaseContext.cs index 6e1fc638..d9815660 100644 --- a/Timeline/Entities/DatabaseContext.cs +++ b/Timeline/Entities/DatabaseContext.cs @@ -28,8 +28,9 @@ namespace Timeline.Entities [Column("version"), Required]
public long Version { get; set; }
- [Required]
public UserAvatar Avatar { get; set; }
+
+ public UserDetailEntity Detail { get; set; }
}
public class DatabaseContext : DbContext
@@ -48,5 +49,6 @@ namespace Timeline.Entities public DbSet<User> Users { get; set; }
public DbSet<UserAvatar> UserAvatars { get; set; }
+ public DbSet<UserDetailEntity> UserDetails { get; set; }
}
}
diff --git a/Timeline/Entities/UserDetail.cs b/Timeline/Entities/UserDetail.cs new file mode 100644 index 00000000..6e582234 --- /dev/null +++ b/Timeline/Entities/UserDetail.cs @@ -0,0 +1,29 @@ +using System.ComponentModel.DataAnnotations;
+using System.ComponentModel.DataAnnotations.Schema;
+
+namespace Timeline.Entities
+{
+ [Table("user_details")]
+ public class UserDetailEntity
+ {
+ [Column("id"), Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
+ public long Id { get; set; }
+
+ [Column("nickname"), MaxLength(15)]
+ public string Nickname { get; set; }
+
+ [Column("qq"), MaxLength(15)]
+ public string QQ { get; set; }
+
+ [Column("email"), MaxLength(50)]
+ public string EMail { get; set; }
+
+ [Column("phone_number"), MaxLength(15)]
+ public string PhoneNumber { get; set; }
+
+ [Column("description")]
+ public string Description { get; set; }
+
+ public long UserId { get; set; }
+ }
+}
diff --git a/Timeline/Migrations/20190822072156_AddUserDetail.Designer.cs b/Timeline/Migrations/20190822072156_AddUserDetail.Designer.cs new file mode 100644 index 00000000..2bbcf673 --- /dev/null +++ b/Timeline/Migrations/20190822072156_AddUserDetail.Designer.cs @@ -0,0 +1,136 @@ +// <auto-generated />
+using System;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Infrastructure;
+using Microsoft.EntityFrameworkCore.Migrations;
+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
+using Timeline.Entities;
+
+namespace Timeline.Migrations
+{
+ [DbContext(typeof(DatabaseContext))]
+ [Migration("20190822072156_AddUserDetail")]
+ partial class AddUserDetail
+ {
+ protected override void BuildTargetModel(ModelBuilder modelBuilder)
+ {
+#pragma warning disable 612, 618
+ modelBuilder
+ .HasAnnotation("ProductVersion", "2.2.6-servicing-10079")
+ .HasAnnotation("Relational:MaxIdentifierLength", 64);
+
+ modelBuilder.Entity("Timeline.Entities.User", b =>
+ {
+ b.Property<long>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnName("id");
+
+ b.Property<string>("EncryptedPassword")
+ .IsRequired()
+ .HasColumnName("password");
+
+ b.Property<string>("Name")
+ .IsRequired()
+ .HasColumnName("name")
+ .HasMaxLength(26);
+
+ b.Property<string>("RoleString")
+ .IsRequired()
+ .HasColumnName("roles");
+
+ b.Property<long>("Version")
+ .ValueGeneratedOnAdd()
+ .HasColumnName("version")
+ .HasDefaultValue(0L);
+
+ b.HasKey("Id");
+
+ b.HasIndex("Name")
+ .IsUnique();
+
+ b.ToTable("users");
+ });
+
+ modelBuilder.Entity("Timeline.Entities.UserAvatar", b =>
+ {
+ b.Property<long>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnName("id");
+
+ b.Property<byte[]>("Data")
+ .HasColumnName("data");
+
+ b.Property<string>("ETag")
+ .HasColumnName("etag")
+ .HasMaxLength(30);
+
+ b.Property<DateTime>("LastModified")
+ .HasColumnName("last_modified");
+
+ b.Property<string>("Type")
+ .HasColumnName("type");
+
+ b.Property<long>("UserId");
+
+ b.HasKey("Id");
+
+ b.HasIndex("UserId")
+ .IsUnique();
+
+ b.ToTable("user_avatars");
+ });
+
+ modelBuilder.Entity("Timeline.Entities.UserDetailEntity", b =>
+ {
+ b.Property<long>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnName("id");
+
+ b.Property<string>("Description")
+ .HasColumnName("description");
+
+ b.Property<string>("EMail")
+ .HasColumnName("email")
+ .HasMaxLength(50);
+
+ b.Property<string>("Nickname")
+ .HasColumnName("nickname")
+ .HasMaxLength(15);
+
+ b.Property<string>("PhoneNumber")
+ .HasColumnName("phone_number")
+ .HasMaxLength(15);
+
+ b.Property<string>("QQ")
+ .HasColumnName("qq")
+ .HasMaxLength(15);
+
+ b.Property<long>("UserId");
+
+ b.HasKey("Id");
+
+ b.HasIndex("UserId")
+ .IsUnique();
+
+ b.ToTable("user_details");
+ });
+
+ modelBuilder.Entity("Timeline.Entities.UserAvatar", b =>
+ {
+ b.HasOne("Timeline.Entities.User")
+ .WithOne("Avatar")
+ .HasForeignKey("Timeline.Entities.UserAvatar", "UserId")
+ .OnDelete(DeleteBehavior.Cascade);
+ });
+
+ modelBuilder.Entity("Timeline.Entities.UserDetailEntity", b =>
+ {
+ b.HasOne("Timeline.Entities.User")
+ .WithOne("Detail")
+ .HasForeignKey("Timeline.Entities.UserDetailEntity", "UserId")
+ .OnDelete(DeleteBehavior.Cascade);
+ });
+#pragma warning restore 612, 618
+ }
+ }
+}
diff --git a/Timeline/Migrations/20190822072156_AddUserDetail.cs b/Timeline/Migrations/20190822072156_AddUserDetail.cs new file mode 100644 index 00000000..4aa6446b --- /dev/null +++ b/Timeline/Migrations/20190822072156_AddUserDetail.cs @@ -0,0 +1,47 @@ +using Microsoft.EntityFrameworkCore.Metadata;
+using Microsoft.EntityFrameworkCore.Migrations;
+
+namespace Timeline.Migrations
+{
+ public partial class AddUserDetail : Migration
+ {
+ protected override void Up(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.CreateTable(
+ name: "user_details",
+ columns: table => new
+ {
+ id = table.Column<long>(nullable: false)
+ .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
+ nickname = table.Column<string>(maxLength: 15, nullable: true),
+ qq = table.Column<string>(maxLength: 15, nullable: true),
+ email = table.Column<string>(maxLength: 50, nullable: true),
+ phone_number = table.Column<string>(maxLength: 15, nullable: true),
+ description = table.Column<string>(nullable: true),
+ UserId = table.Column<long>(nullable: false)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_user_details", x => x.id);
+ table.ForeignKey(
+ name: "FK_user_details_users_UserId",
+ column: x => x.UserId,
+ principalTable: "users",
+ principalColumn: "id",
+ onDelete: ReferentialAction.Cascade);
+ });
+
+ migrationBuilder.CreateIndex(
+ name: "IX_user_details_UserId",
+ table: "user_details",
+ column: "UserId",
+ unique: true);
+ }
+
+ protected override void Down(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.DropTable(
+ name: "user_details");
+ }
+ }
+}
diff --git a/Timeline/Migrations/DatabaseContextModelSnapshot.cs b/Timeline/Migrations/DatabaseContextModelSnapshot.cs index 4941321c..1328b855 100644 --- a/Timeline/Migrations/DatabaseContextModelSnapshot.cs +++ b/Timeline/Migrations/DatabaseContextModelSnapshot.cs @@ -78,6 +78,41 @@ namespace Timeline.Migrations b.ToTable("user_avatars");
});
+ modelBuilder.Entity("Timeline.Entities.UserDetailEntity", b =>
+ {
+ b.Property<long>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnName("id");
+
+ b.Property<string>("Description")
+ .HasColumnName("description");
+
+ b.Property<string>("EMail")
+ .HasColumnName("email")
+ .HasMaxLength(50);
+
+ b.Property<string>("Nickname")
+ .HasColumnName("nickname")
+ .HasMaxLength(15);
+
+ b.Property<string>("PhoneNumber")
+ .HasColumnName("phone_number")
+ .HasMaxLength(15);
+
+ b.Property<string>("QQ")
+ .HasColumnName("qq")
+ .HasMaxLength(15);
+
+ b.Property<long>("UserId");
+
+ b.HasKey("Id");
+
+ b.HasIndex("UserId")
+ .IsUnique();
+
+ b.ToTable("user_details");
+ });
+
modelBuilder.Entity("Timeline.Entities.UserAvatar", b =>
{
b.HasOne("Timeline.Entities.User")
@@ -85,6 +120,14 @@ namespace Timeline.Migrations .HasForeignKey("Timeline.Entities.UserAvatar", "UserId")
.OnDelete(DeleteBehavior.Cascade);
});
+
+ modelBuilder.Entity("Timeline.Entities.UserDetailEntity", b =>
+ {
+ b.HasOne("Timeline.Entities.User")
+ .WithOne("Detail")
+ .HasForeignKey("Timeline.Entities.UserDetailEntity", "UserId")
+ .OnDelete(DeleteBehavior.Cascade);
+ });
#pragma warning restore 612, 618
}
}
diff --git a/Timeline/Models/UserDetail.cs b/Timeline/Models/UserDetail.cs new file mode 100644 index 00000000..86866d8b --- /dev/null +++ b/Timeline/Models/UserDetail.cs @@ -0,0 +1,43 @@ +using System.ComponentModel.DataAnnotations;
+using Timeline.Entities;
+using Timeline.Models.Validation;
+
+namespace Timeline.Models
+{
+ public class UserDetail
+ {
+ [MaxLength(10)]
+ public string Nickname { get; set; }
+
+ [ValidateWith(typeof(UserDetailValidators.QQValidator))]
+ public string QQ { get; set; }
+
+ [ValidateWith(typeof(UserDetailValidators.EMailValidator))]
+ public string EMail { get; set; }
+
+ [ValidateWith(typeof(UserDetailValidators.PhoneNumberValidator))]
+ public string PhoneNumber { get; set; }
+
+ public string Description { get; set; }
+
+ private static string CoerceEmptyToNull(string value)
+ {
+ if (string.IsNullOrEmpty(value))
+ return null;
+ else
+ return value;
+ }
+
+ public static UserDetail From(UserDetailEntity entity)
+ {
+ return new UserDetail
+ {
+ Nickname = CoerceEmptyToNull(entity.Nickname),
+ QQ = CoerceEmptyToNull(entity.QQ),
+ EMail = CoerceEmptyToNull(entity.EMail),
+ PhoneNumber = CoerceEmptyToNull(entity.PhoneNumber),
+ Description = CoerceEmptyToNull(entity.Description)
+ };
+ }
+ }
+}
diff --git a/Timeline/Models/Validation/UserDetailValidator.cs b/Timeline/Models/Validation/UserDetailValidator.cs new file mode 100644 index 00000000..19c82edb --- /dev/null +++ b/Timeline/Models/Validation/UserDetailValidator.cs @@ -0,0 +1,116 @@ +using System;
+using System.Net.Mail;
+
+namespace Timeline.Models.Validation
+{
+ public abstract class OptionalStringValidator : IValidator
+ {
+ public bool Validate(object value, out string message)
+ {
+ if (value == null)
+ {
+ message = ValidationConstants.SuccessMessage;
+ return true;
+ }
+
+ if (value is string s)
+ {
+ if (s.Length == 0)
+ {
+ message = ValidationConstants.SuccessMessage;
+ return true;
+ }
+ return DoValidate(s, out message);
+ }
+ else
+ {
+ message = "Value is not of type string.";
+ return false;
+ }
+ }
+
+ protected abstract bool DoValidate(string value, out string message);
+ }
+
+ public static class UserDetailValidators
+ {
+
+ public class QQValidator : OptionalStringValidator
+ {
+ protected override bool DoValidate(string value, out string message)
+ {
+ if (value.Length < 5)
+ {
+ message = "QQ is too short.";
+ return false;
+ }
+
+ if (value.Length > 11)
+ {
+ message = "QQ is too long.";
+ return false;
+ }
+
+ foreach (var c in value)
+ {
+ if (!char.IsDigit(c))
+ {
+ message = "QQ must only contain digit.";
+ return false;
+ }
+ }
+
+ message = ValidationConstants.SuccessMessage;
+ return true;
+ }
+ }
+
+ public class EMailValidator : OptionalStringValidator
+ {
+ protected override bool DoValidate(string value, out string message)
+ {
+ if (value.Length > 50)
+ {
+ message = "E-Mail is too long.";
+ return false;
+ }
+
+ try
+ {
+ var _ = new MailAddress(value);
+ }
+ catch (FormatException)
+ {
+ message = "The format of E-Mail is bad.";
+ return false;
+ }
+ message = ValidationConstants.SuccessMessage;
+ return true;
+ }
+ }
+
+ public class PhoneNumberValidator : OptionalStringValidator
+ {
+ protected override bool DoValidate(string value, out string message)
+ {
+ if (value.Length > 14)
+ {
+ message = "Phone number is too long.";
+ return false;
+ }
+
+ foreach (var c in value)
+ {
+ if (!char.IsDigit(c))
+ {
+ message = "Phone number can only contain digit.";
+ return false;
+ }
+ }
+
+ message = ValidationConstants.SuccessMessage;
+ return true;
+ }
+ }
+ }
+}
diff --git a/Timeline/Services/DatabaseExtensions.cs b/Timeline/Services/DatabaseExtensions.cs new file mode 100644 index 00000000..a37cf05b --- /dev/null +++ b/Timeline/Services/DatabaseExtensions.cs @@ -0,0 +1,30 @@ +using Microsoft.EntityFrameworkCore;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using Timeline.Entities;
+
+namespace Timeline.Services
+{
+ public static class DatabaseExtensions
+ {
+ /// <summary>
+ /// Check the existence and get the id of the user.
+ /// </summary>
+ /// <param name="username">The username of the user.</param>
+ /// <returns>The user id.</returns>
+ /// <exception cref="ArgumentException">Thrown if <paramref name="username"/> is null or empty.</exception>
+ /// <exception cref="UserNotExistException">Thrown if user does not exist.</exception>
+ public static async Task<long> CheckAndGetUser(DbSet<User> userDbSet, string username)
+ {
+ if (string.IsNullOrEmpty(username))
+ throw new ArgumentException("Username is null or empty.", nameof(username));
+
+ var userId = await userDbSet.Where(u => u.Name == username).Select(u => u.Id).SingleOrDefaultAsync();
+ if (userId == 0)
+ throw new UserNotExistException(username);
+ return userId;
+ }
+ }
+}
diff --git a/Timeline/Services/UserAvatarService.cs b/Timeline/Services/UserAvatarService.cs index 7b1f405c..5c380dd8 100644 --- a/Timeline/Services/UserAvatarService.cs +++ b/Timeline/Services/UserAvatarService.cs @@ -219,12 +219,7 @@ namespace Timeline.Services public async Task<string> GetAvatarETag(string username)
{
- if (string.IsNullOrEmpty(username))
- throw new ArgumentException("Username is null or empty.", nameof(username));
-
- var userId = await _database.Users.Where(u => u.Name == username).Select(u => u.Id).SingleOrDefaultAsync();
- if (userId == 0)
- throw new UserNotExistException(username);
+ var userId = await DatabaseExtensions.CheckAndGetUser(_database.Users, username);
var eTag = (await _database.UserAvatars.Where(a => a.UserId == userId).Select(a => new { a.ETag }).SingleAsync()).ETag;
if (eTag == null)
@@ -235,12 +230,7 @@ namespace Timeline.Services public async Task<AvatarInfo> GetAvatar(string username)
{
- if (string.IsNullOrEmpty(username))
- throw new ArgumentException("Username is null or empty.", nameof(username));
-
- var userId = await _database.Users.Where(u => u.Name == username).Select(u => u.Id).SingleOrDefaultAsync();
- if (userId == 0)
- throw new UserNotExistException(username);
+ var userId = await DatabaseExtensions.CheckAndGetUser(_database.Users, username);
var avatar = await _database.UserAvatars.Where(a => a.UserId == userId).Select(a => new { a.Type, a.Data, a.LastModified }).SingleAsync();
@@ -272,9 +262,6 @@ namespace Timeline.Services public async Task SetAvatar(string username, Avatar avatar)
{
- if (string.IsNullOrEmpty(username))
- throw new ArgumentException("Username is null or empty.", nameof(username));
-
if (avatar != null)
{
if (string.IsNullOrEmpty(avatar.Type))
@@ -283,9 +270,7 @@ namespace Timeline.Services throw new ArgumentException("Data of avatar is null.", nameof(avatar));
}
- var userId = await _database.Users.Where(u => u.Name == username).Select(u => u.Id).SingleOrDefaultAsync();
- if (userId == 0)
- throw new UserNotExistException(username);
+ var userId = await DatabaseExtensions.CheckAndGetUser(_database.Users, username);
var avatarEntity = await _database.UserAvatars.Where(a => a.UserId == userId).SingleAsync();
diff --git a/Timeline/Services/UserDetailService.cs b/Timeline/Services/UserDetailService.cs new file mode 100644 index 00000000..a8ed662b --- /dev/null +++ b/Timeline/Services/UserDetailService.cs @@ -0,0 +1,135 @@ +using Microsoft.EntityFrameworkCore;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Logging;
+using System;
+using System.Linq;
+using System.Threading.Tasks;
+using Timeline.Entities;
+using Timeline.Models;
+
+namespace Timeline.Services
+{
+ public interface IUserDetailService
+ {
+ /// <summary>
+ /// Get the nickname of user.
+ /// </summary>
+ /// <param name="username">The username to get nickname of.</param>
+ /// <returns>The user's nickname. Null if not set.</returns>
+ /// <exception cref="ArgumentException">Thrown if <paramref name="username"/> is null or empty.</exception>
+ /// <exception cref="UserNotExistException">Thrown if user doesn't exist.</exception>
+ Task<string> GetUserNickname(string username);
+
+ /// <summary>
+ /// Get the detail of user.
+ /// </summary>
+ /// <param name="username">The username to get user detail of.</param>
+ /// <returns>The user detail.</returns>
+ /// <exception cref="ArgumentException">Thrown if <paramref name="username"/> is null or empty.</exception>
+ /// <exception cref="UserNotExistException">Thrown if user doesn't exist.</exception>
+ Task<UserDetail> GetUserDetail(string username);
+
+ /// <summary>
+ /// Update the detail of user. This function does not do data check.
+ /// </summary>
+ /// <param name="username">The username to get user detail of.</param>
+ /// <param name="detail">The detail to update. Can't be null. Any null member means not set.</param>
+ /// <exception cref="ArgumentException">Thrown if <paramref name="username"/> is null or empty or <paramref name="detail"/> is null.</exception>
+ /// <exception cref="UserNotExistException">Thrown if user doesn't exist.</exception>
+ Task UpdateUserDetail(string username, UserDetail detail);
+ }
+
+ public class UserDetailService : IUserDetailService
+ {
+ private readonly ILogger<UserDetailService> _logger;
+
+ private readonly DatabaseContext _databaseContext;
+
+ public UserDetailService(ILogger<UserDetailService> logger, DatabaseContext databaseContext)
+ {
+ _logger = logger;
+ _databaseContext = databaseContext;
+ }
+
+ private async Task<UserDetailEntity> CreateEntity(long userId)
+ {
+ var entity = new UserDetailEntity()
+ {
+ UserId = userId
+ };
+ _databaseContext.UserDetails.Add(entity);
+ await _databaseContext.SaveChangesAsync();
+ _logger.LogInformation("An entity is created in user_details.");
+ return entity;
+ }
+
+ // Check the existence of user detail entry
+ private async Task<UserDetailEntity> CheckAndInit(long userId)
+ {
+ var detail = await _databaseContext.UserDetails.Where(e => e.UserId == userId).SingleOrDefaultAsync();
+ if (detail == null)
+ {
+ detail = await CreateEntity(userId);
+ }
+ return detail;
+ }
+
+ public async Task<string> GetUserNickname(string username)
+ {
+ var userId = await DatabaseExtensions.CheckAndGetUser(_databaseContext.Users, username);
+ var detail = await _databaseContext.UserDetails.Where(e => e.UserId == userId).Select(e => new { e.Nickname }).SingleOrDefaultAsync();
+ if (detail == null)
+ {
+ var entity = await CreateEntity(userId);
+ return null;
+ }
+ else
+ {
+ var nickname = detail.Nickname;
+ return string.IsNullOrEmpty(nickname) ? null : nickname;
+ }
+ }
+
+ public async Task<UserDetail> GetUserDetail(string username)
+ {
+ var userId = await DatabaseExtensions.CheckAndGetUser(_databaseContext.Users, username);
+ var detailEntity = await CheckAndInit(userId);
+ return UserDetail.From(detailEntity);
+ }
+
+ public async Task UpdateUserDetail(string username, UserDetail detail)
+ {
+ if (detail == null)
+ throw new ArgumentNullException(nameof(detail));
+
+ var userId = await DatabaseExtensions.CheckAndGetUser(_databaseContext.Users, username);
+ var detailEntity = await CheckAndInit(userId);
+
+ if (detail.Nickname != null)
+ detailEntity.Nickname = detail.Nickname;
+
+ if (detail.QQ != null)
+ detailEntity.QQ = detail.QQ;
+
+ if (detail.EMail != null)
+ detailEntity.EMail = detail.EMail;
+
+ if (detail.PhoneNumber != null)
+ detailEntity.PhoneNumber = detail.PhoneNumber;
+
+ if (detail.Description != null)
+ detailEntity.Description = detail.Description;
+
+ await _databaseContext.SaveChangesAsync();
+ _logger.LogInformation("An entity is updated in user_details.");
+ }
+ }
+
+ public static class UserDetailServiceCollectionExtensions
+ {
+ public static void AddUserDetailService(this IServiceCollection services)
+ {
+ services.AddScoped<IUserDetailService, UserDetailService>();
+ }
+ }
+}
diff --git a/Timeline/Startup.cs b/Timeline/Startup.cs index 66f648c3..b5a5106b 100644 --- a/Timeline/Startup.cs +++ b/Timeline/Startup.cs @@ -46,6 +46,7 @@ namespace Timeline services.AddTransient<IClock, Clock>();
services.AddUserAvatarService();
+ services.AddUserDetailService();
var databaseConfig = Configuration.GetSection(nameof(DatabaseConfig)).Get<DatabaseConfig>();
|