diff --git a/CyberBoom/Controllers/UserController.cs b/CyberBoom/Controllers/UserController.cs index fc95be2..87ccec7 100644 --- a/CyberBoom/Controllers/UserController.cs +++ b/CyberBoom/Controllers/UserController.cs @@ -3,6 +3,7 @@ using Mapster; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Google; using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; @@ -11,23 +12,38 @@ namespace CyberBoom.Controllers; [ApiController] [Route("/api/[controller]")] -public class UserController : ControllerBase +public class UsersController : ControllerBase { - private static readonly string[] Summaries = new[] - { - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" - }; + - private readonly ILogger _logger; + private readonly ApplicationContext _applicationContext; - public UserController(ILogger logger, ApplicationContext applicationContext) + private readonly UserManager _userManager; + + public UsersController(ApplicationContext applicationContext, UserManager userManager) { - _logger = logger; _applicationContext = applicationContext; + _userManager = userManager; } + [HttpPost] + public async Task Post([FromForm]UserPost user) + { + await user.Avatar.WriteFileToDirectory(); + var userWr = new User { + AvatarUrl = user.Avatar.FileName, + UserName = user.Username + }; + await _userManager.CreateAsync(userWr); + + return Ok( + new { + userWr.Id + } + ); + } // [HttpGet("google-auth")] // public IActionResult Regiester() @@ -66,18 +82,15 @@ public class UserController : ControllerBase [Route("/api/[controller]")] public class MeetingsController : ControllerBase { - private static readonly string[] Summaries = new[] - { - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" - }; + - private readonly ILogger _logger; private readonly ApplicationContext _applicationContext; - public MeetingsController(ILogger logger, ApplicationContext applicationContext) + public MeetingsController(ApplicationContext applicationContext) { - _logger = logger; + + _applicationContext = applicationContext; } @@ -85,8 +98,8 @@ public class MeetingsController : ControllerBase [HttpPost] public async Task Post([FromForm]PostMeetingDto meeting) { - await meeting.SpeackerImage.WriteFileToDirectory(); - await meeting.PlaceImages.WriteFileToDirectory(); + await meeting.SpeackerImage.WriteFilesToDirectory(); + await meeting.PlaceImages.WriteFilesToDirectory(); var meetingWrite = meeting.Adapt(); meetingWrite.SpeackerImage = meeting.SpeackerImage.JoinFileNames(); @@ -105,8 +118,8 @@ public class MeetingsController : ControllerBase [HttpPut] public async Task Put([FromForm]PutMeetingDto meeting) { - await meeting.SpeackerImage.WriteFileToDirectory(); - await meeting.PlaceImages.WriteFileToDirectory(); + await meeting.SpeackerImage.WriteFilesToDirectory(); + await meeting.PlaceImages.WriteFilesToDirectory(); var meetingWrite = meeting.Adapt(); @@ -139,3 +152,213 @@ public class MeetingsController : ControllerBase return Ok(meetings); } } + + + +[ApiController] +[Route("/api/[controller]")] +public class ReviewsController : ControllerBase +{ + + private readonly ApplicationContext _applicationContext; + + public ReviewsController(ApplicationContext applicationContext) + { + _applicationContext = applicationContext; + } + + + [HttpPost] + public async Task Post([FromBody] PostReviewDto review) + { + var dbWr = review.Adapt(); + + await _applicationContext.Reviews.AddAsync(dbWr); + + await _applicationContext.SaveChangesAsync(); + + return Ok(new { + dbWr.Id + }); + } + + [HttpPut] + public async Task Put([FromBody]PutReviewDto review) + { + + var fReview = await _applicationContext.Reviews.FirstAsync(r => r.Id == review.Id); + + + fReview.Text = review.Text; + fReview.Score = review.Score; + fReview.Date = review.Date; + + + await _applicationContext.SaveChangesAsync(); + + return Ok(); + } + + [HttpGet] + public async Task Get(int id) + { + var review = await _applicationContext.Reviews + .Include(c => c.User) + .FirstAsync(s => s.Id == id); + + + return Ok(review); + } + + + [HttpGet("list")] + public IActionResult GetList(int offset, int limit) + { + var reviews = _applicationContext.Reviews + .Include(c => c.User) + .Skip(offset) + .Take(limit); + + + return Ok(reviews); + } +} + + + +[ApiController] +[Route("/api/[controller]")] +public class ReactionsController : ControllerBase +{ + + private readonly ApplicationContext _applicationContext; + + public ReactionsController(ApplicationContext applicationContext) + { + _applicationContext = applicationContext; + } + + + [HttpPost] + public async Task Post([FromBody] PostReactionDto reaction) + { + var dbWr = reaction.Adapt(); + + await _applicationContext.Reactions.AddAsync(dbWr); + + await _applicationContext.SaveChangesAsync(); + + return Ok(new { + dbWr.Id + }); + } + + [HttpDelete] + public async Task Delete(long id) + { + + var fReview = await _applicationContext.Reactions.FirstAsync(r => r.Id == id); + + + _applicationContext.Reactions.Remove(fReview); + + + await _applicationContext.SaveChangesAsync(); + + return Ok(); + } + + [HttpGet] + public async Task Get(int id) + { + var review = await _applicationContext.Reviews + .Include(c => c.User) + .FirstAsync(s => s.Id == id); + + + return Ok(review); + } + + + [HttpGet("list")] + public IActionResult GetList(int offset, int limit) + { + var reviews = _applicationContext.Reviews + .Include(c => c.User) + .Skip(offset) + .Take(limit); + + + return Ok(reviews); + } +} + + + + + +[ApiController] +[Route("/api/users/meetings")] +public class UserWriteToMetingController : ControllerBase +{ + + private readonly ApplicationContext _applicationContext; + + public UserWriteToMetingController(ApplicationContext applicationContext) + { + _applicationContext = applicationContext; + } + + + [HttpPost] + public async Task Post([FromBody] PostUserWriteToMetingDto write) + { + var dbWr = write.Adapt(); + + await _applicationContext.UserWriteToMetings.AddAsync(dbWr); + + await _applicationContext.SaveChangesAsync(); + + return Ok(new { + dbWr.Id + }); + } + + [HttpDelete] + public async Task Delete(long id) + { + + var fReview = await _applicationContext.UserWriteToMetings.FirstAsync(r => r.Id == id); + + + _applicationContext.UserWriteToMetings.Remove(fReview); + + + await _applicationContext.SaveChangesAsync(); + + return Ok(); + } + + [HttpGet] + public async Task Get(int id) + { + var review = await _applicationContext.UserWriteToMetings + .FirstAsync(s => s.Id == id); + + + return Ok(review); + } + + + [HttpGet("list")] + public IActionResult GetList(int offset, int limit) + { + var reviews = _applicationContext.UserWriteToMetings + .Skip(offset) + .Take(limit); + + + return Ok(reviews); + } +} + diff --git a/CyberBoom/DbContext/User.cs b/CyberBoom/DbContext/User.cs index 74cfb9a..c0a6130 100644 --- a/CyberBoom/DbContext/User.cs +++ b/CyberBoom/DbContext/User.cs @@ -4,7 +4,14 @@ using Microsoft.EntityFrameworkCore; public class User : IdentityUser { - + public string AvatarUrl { get; set; } = null!; +} + +public class UserPost +{ + public IFormFile Avatar { get; set; } = null!; + + public string Username { get; set; } = null!; } public class PostMeetingDto @@ -36,6 +43,24 @@ public class PostMeetingDto } +public class UserWriteToMeting +{ + public long Id { get; set; } + + public string UserId { get; set; } = null!; + + public long MeetingId { get; set; } +} + + +public class PostUserWriteToMetingDto +{ + public string UserId { get; set; } = null!; + + public long MeetingId { get; set; } +} + + public class PutMeetingDto { public long Id { get; set; } @@ -95,12 +120,110 @@ public class Meeting public string Tags { get; set; } = null!; public string VideoUrl { get; set; } = null!; + + + // public string MeetingChatUrl { get; set; } = null!; + + // public string SocialUrls { get; set; } = null!; +} + + +public class PostReviewDto +{ + public long MeetingId { get; set; } + + public string UserId { get; set; } = null!; + + public string Text { get; set; } = null!; + + public int Score { get; set; } = 0; + + DateTime _date; + + public DateTime Date { get => _date; set => _date = value.ToUniversalTime(); } } + +public class PutReviewDto +{ + public long Id { get; set; } + + public string Text { get; set; } = null!; + + public int Score { get; set; } = 0; + + DateTime _date; + + public DateTime Date { get => _date; set => _date = value.ToUniversalTime(); } +} + + +public class Review +{ + public long Id { get; set; } + + public long MeetingId { get; set; } + + public User User { get; set; } = null!; + + public string UserId { get; set; } = null!; + + public string Text { get; set; } = null!; + + public int Score { get; set; } = 0; + + DateTime _date; + + public DateTime Date { get => _date; set => _date = value.ToUniversalTime(); } +} + + + +public class PostReactionDto +{ + + public long ReviewId { get; set; } + + public string UserId { get; set; } = null!; + + public bool IsLike { get; set; } = true; +} + +public class PutReactionDto +{ + public long Id { get; set; } + + public long ReviewId { get; set; } + + public string UserId { get; set; } = null!; + + public bool IsLike { get; set; } = true; +} + + +public class Reaction +{ + public long Id { get; set; } + + public long ReviewId { get; set; } + + public string UserId { get; set; } = null!; + + public bool IsLike { get; set; } = true; +} + + + public class ApplicationContext : IdentityDbContext { public DbSet Meetings { get; set; } + + public DbSet Reviews { get; set; } + + public DbSet Reactions { get; set; } + public DbSet UserWriteToMetings { get; set; } + public ApplicationContext(DbContextOptions options) : base(options) { @@ -110,6 +233,11 @@ public class ApplicationContext : IdentityDbContext protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); - builder.Entity(); + builder.Entity().HasMany().WithOne().HasForeignKey(c => c.MeetingId); + builder.Entity().HasMany().WithOne(r => r.User).HasForeignKey(c => c.UserId); + builder.Entity().HasMany().WithOne().HasForeignKey(c => c.UserId); + builder.Entity().HasMany().WithOne().HasForeignKey(c => c.ReviewId); + builder.Entity().HasMany().WithOne().HasForeignKey(c => c.MeetingId); + builder.Entity().HasMany().WithOne().HasForeignKey(c => c.UserId); } } \ No newline at end of file diff --git a/CyberBoom/Program.cs b/CyberBoom/Program.cs index 3df3d27..6706ee2 100644 --- a/CyberBoom/Program.cs +++ b/CyberBoom/Program.cs @@ -114,17 +114,19 @@ public static class PhileDataHelpers public static string JoinStrings(this IEnumerable files) => String.Join(FILES_SEPORATOR_IN_STORE, files.Select(s => s)); - - public static async Task WriteFileToDirectory(this IEnumerable files) + public static async Task WriteFileToDirectory(this IFormFile file) { - var dir = Directory.CreateDirectory("cyber-boom-files"); - + var readStream = file.OpenReadStream(); + var memstream = new MemoryStream(); + await readStream.CopyToAsync(memstream); + await File.WriteAllBytesAsync(Path.Combine("cyber-boom-files", file.FileName), memstream.ToArray()); + } + + public static async Task WriteFilesToDirectory(this IEnumerable files) + { foreach(var file in files) { - var readStream = file.OpenReadStream(); - var memstream = new MemoryStream(); - await readStream.CopyToAsync(memstream); - await File.WriteAllBytesAsync(Path.Combine(dir.FullName, file.FileName), memstream.ToArray()); + await file.WriteFileToDirectory(); } } diff --git a/CyberBoom/appsettings.json b/CyberBoom/appsettings.json index 060cc32..9f85f41 100644 --- a/CyberBoom/appsettings.json +++ b/CyberBoom/appsettings.json @@ -7,7 +7,7 @@ } }, "AllowedHosts": "*", - "ConnectionStrings": { - "DefaultConnection": "Host=localhost; Database=CyberBoomWellBeing; Username=postgres; Password=supper_password_123" - } + + "CONNECTION_STRING": "Host=localhost; Database=CyberBoomWellBeing; Username=postgres; Password=supper_password_123" + }