добавил логики
This commit is contained in:
		| @@ -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<UserController> _logger; | ||||
|      | ||||
|  | ||||
|     private readonly ApplicationContext _applicationContext; | ||||
|  | ||||
|     public UserController(ILogger<UserController> logger, ApplicationContext applicationContext) | ||||
|     private readonly UserManager<User> _userManager; | ||||
|  | ||||
|     public UsersController(ApplicationContext applicationContext, UserManager<User> userManager) | ||||
|     { | ||||
|         _logger = logger; | ||||
|         _applicationContext = applicationContext; | ||||
|         _userManager = userManager; | ||||
|     } | ||||
|  | ||||
|     [HttpPost] | ||||
|     public async Task<IActionResult> 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<UserController> _logger; | ||||
|  | ||||
|     private readonly ApplicationContext _applicationContext; | ||||
|  | ||||
|     public MeetingsController(ILogger<UserController> logger, ApplicationContext applicationContext) | ||||
|     public MeetingsController(ApplicationContext applicationContext) | ||||
|     { | ||||
|         _logger = logger; | ||||
|          | ||||
|          | ||||
|         _applicationContext = applicationContext; | ||||
|     } | ||||
|  | ||||
| @@ -85,8 +98,8 @@ public class MeetingsController : ControllerBase | ||||
|     [HttpPost] | ||||
|     public async Task<IActionResult> Post([FromForm]PostMeetingDto meeting) | ||||
|     { | ||||
|         await meeting.SpeackerImage.WriteFileToDirectory(); | ||||
|         await meeting.PlaceImages.WriteFileToDirectory(); | ||||
|         await meeting.SpeackerImage.WriteFilesToDirectory(); | ||||
|         await meeting.PlaceImages.WriteFilesToDirectory(); | ||||
|         var meetingWrite = meeting.Adapt<Meeting>(); | ||||
|          | ||||
|         meetingWrite.SpeackerImage = meeting.SpeackerImage.JoinFileNames(); | ||||
| @@ -105,8 +118,8 @@ public class MeetingsController : ControllerBase | ||||
|     [HttpPut] | ||||
|     public async Task<IActionResult> Put([FromForm]PutMeetingDto meeting) | ||||
|     { | ||||
|         await meeting.SpeackerImage.WriteFileToDirectory(); | ||||
|         await meeting.PlaceImages.WriteFileToDirectory(); | ||||
|         await meeting.SpeackerImage.WriteFilesToDirectory(); | ||||
|         await meeting.PlaceImages.WriteFilesToDirectory(); | ||||
|  | ||||
|         var meetingWrite = meeting.Adapt<Meeting>(); | ||||
|          | ||||
| @@ -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<IActionResult> Post([FromBody] PostReviewDto review) | ||||
|     { | ||||
|         var dbWr = review.Adapt<Review>(); | ||||
|          | ||||
|         await _applicationContext.Reviews.AddAsync(dbWr); | ||||
|          | ||||
|         await _applicationContext.SaveChangesAsync(); | ||||
|          | ||||
|         return Ok(new { | ||||
|             dbWr.Id | ||||
|         }); | ||||
|     } | ||||
|  | ||||
|     [HttpPut] | ||||
|     public async Task<IActionResult> 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<IActionResult> 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<IActionResult> Post([FromBody] PostReactionDto reaction) | ||||
|     { | ||||
|         var dbWr = reaction.Adapt<Reaction>(); | ||||
|          | ||||
|         await _applicationContext.Reactions.AddAsync(dbWr); | ||||
|          | ||||
|         await _applicationContext.SaveChangesAsync(); | ||||
|          | ||||
|         return Ok(new { | ||||
|             dbWr.Id | ||||
|         }); | ||||
|     } | ||||
|  | ||||
|     [HttpDelete] | ||||
|     public async Task<IActionResult> 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<IActionResult> 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<IActionResult> Post([FromBody] PostUserWriteToMetingDto write) | ||||
|     { | ||||
|         var dbWr = write.Adapt<UserWriteToMeting>(); | ||||
|          | ||||
|         await _applicationContext.UserWriteToMetings.AddAsync(dbWr); | ||||
|          | ||||
|         await _applicationContext.SaveChangesAsync(); | ||||
|          | ||||
|         return Ok(new { | ||||
|             dbWr.Id | ||||
|         }); | ||||
|     } | ||||
|  | ||||
|     [HttpDelete] | ||||
|     public async Task<IActionResult> 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<IActionResult> 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); | ||||
|     } | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -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<User> | ||||
| { | ||||
|     public DbSet<Meeting> Meetings { get; set; } | ||||
|  | ||||
|     public DbSet<Review> Reviews { get; set; } | ||||
|  | ||||
|     public DbSet<Reaction> Reactions { get; set; } | ||||
|     public DbSet<UserWriteToMeting> UserWriteToMetings { get; set; } | ||||
|  | ||||
|     public ApplicationContext(DbContextOptions<ApplicationContext> options) | ||||
|         : base(options) | ||||
|     { | ||||
| @@ -110,6 +233,11 @@ public class ApplicationContext : IdentityDbContext<User> | ||||
|     protected override void OnModelCreating(ModelBuilder builder) | ||||
|     { | ||||
|         base.OnModelCreating(builder); | ||||
|         builder.Entity<Meeting>(); | ||||
|         builder.Entity<Meeting>().HasMany<Review>().WithOne().HasForeignKey(c => c.MeetingId); | ||||
|         builder.Entity<User>().HasMany<Review>().WithOne(r => r.User).HasForeignKey(c => c.UserId); | ||||
|         builder.Entity<User>().HasMany<Reaction>().WithOne().HasForeignKey(c => c.UserId); | ||||
|         builder.Entity<Review>().HasMany<Reaction>().WithOne().HasForeignKey(c => c.ReviewId); | ||||
|         builder.Entity<Meeting>().HasMany<UserWriteToMeting>().WithOne().HasForeignKey(c => c.MeetingId); | ||||
|         builder.Entity<User>().HasMany<UserWriteToMeting>().WithOne().HasForeignKey(c => c.UserId); | ||||
|     } | ||||
| } | ||||
| @@ -114,17 +114,19 @@ public static class PhileDataHelpers | ||||
|  | ||||
|     public static string JoinStrings(this IEnumerable<string> files) => String.Join(FILES_SEPORATOR_IN_STORE, files.Select(s => s)); | ||||
|  | ||||
|  | ||||
|     public static async Task WriteFileToDirectory(this IEnumerable<IFormFile> 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<IFormFile> 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(); | ||||
|         } | ||||
|          | ||||
|     } | ||||
|   | ||||
| @@ -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" | ||||
|  | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user