public async Task <IActionResult> NewUserPhoto([FromForm] IFormFile file) { var userId = GetUserId(); var user = _context.Users.Find(userId); if (file == null) { return(new OkObjectResult(new { success = false, error = "Null file" })); } var supportedTypes = new[] { "image/jpeg", "image/png", "image/gif" }; if (!supportedTypes.Contains(file.ContentType)) { return(new OkObjectResult(new { success = false, error = "Unsupported file type, please use an image." })); } Directory.CreateDirectory(UserPhotoDir); var fileName = ContentDispositionHeaderValue .Parse(file.ContentDisposition) .FileName; var dateTimeHash = DateTime.Now.Ticks .GetHashCode() .ToString("x"); var ext = Path.GetExtension(fileName).ToString(); var url = Path.Combine( UserPhotoDir, $"user{userId}_{dateTimeHash}{ext}"); var oldPhoto = user.PhotoUrl; if (oldPhoto != null) { // remove leading slash var photoLocation = oldPhoto.Remove(0, 1); if (System.IO.File.Exists(photoLocation)) { System.IO.File.Delete(photoLocation); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("photo gone"); } } using (var fs = System.IO.File.Create(url)) { await file.CopyToAsync(fs); fs.Flush(); } user.PhotoUrl = $"/{url}"; _context.SaveChanges(); return(new OkObjectResult(new { success = true, url })); }
public void MarkNotificationSeen([FromBody] NotificationDto dto) { var notification = _context.Notifications.Find(dto.NotificationId); notification.Status = NotificationStatus.Seen; _context.Update(notification); _context.SaveChanges(); }
public OkObjectResult NewEnsemble([FromBody] EnsembleDto dto) { var userId = GetUserId(); var ensemble = new Ensemble { Name = dto.Name }; _context.Ensembles.Add(ensemble); // Make ensemble creator Mod var mod = new EnsembleModerator { UserIdRecipient = userId, UserIdRequester = userId, EnsembleId = ensemble.EnsembleId, Status = RequestStatus.Accepted, ConfirmedAt = DateTime.Now }; _context.EnsembleModerators.Add(mod); // Make ensemble creator Member var mem = new EnsembleMember { EnsembleId = ensemble.EnsembleId, UserIdRecipient = userId, UserIdRequester = userId, Status = RequestStatus.Accepted, ConfirmedAt = DateTime.Now }; _context.EnsembleMembers.Add(mem); _context.SaveChanges(); return(new OkObjectResult(new { success = true, ensembleId = ensemble.EnsembleId })); }
public OkObjectResult NewEvent([FromBody] EventDto dto) { var userId = GetUserId(); var ev = new Event { Name = dto.Name, Location = dto.Location, CreatedByUserId = userId, DateAndTime = dto.DateAndTime }; _context.Events.Add(ev); _context.EventModerators.Add(new EventModerator { EventId = ev.EventId, UserIdRecipient = userId, UserIdRequester = userId, Status = RequestStatus.Accepted }); _context.SaveChanges(); return(new OkObjectResult(new { success = true, eventId = ev.EventId })); }
public OkObjectResult ConfirmRequest([FromBody] RequestDto dto) { var userId = GetUserId(); var success = false; switch (dto.RequestType) { case RequestType.Booking: var b = _context.Bookings.Find(dto.TypeId); if (b.UserIdRecipient != userId) { break; } b.ConfirmedAt = DateTime.Now; b.Status = RequestStatus.Accepted; _context.SaveChanges(); success = true; break; case RequestType.Connection: var c = _context.Connections.Find(dto.TypeId); if (c.UserIdRecipient != userId) { break; } c.ConfirmedAt = DateTime.Now; c.Status = RequestStatus.Accepted; _context.SaveChanges(); success = true; break; case RequestType.EnsembleMember: var enMem = _context.EnsembleMembers.Find(dto.TypeId); if (enMem.UserIdRecipient != userId) { break; } enMem.ConfirmedAt = DateTime.Now; enMem.Status = RequestStatus.Accepted; _context.SaveChanges(); success = true; break; case RequestType.EventModerator: var evMod = _context.EventModerators.Find(dto.TypeId); if (evMod.UserIdRecipient != userId) { break; } evMod.ConfirmedAt = DateTime.Now; evMod.Status = RequestStatus.Accepted; _context.SaveChanges(); success = true; break; case RequestType.EnsembleModerator: var enMod = _context.EnsembleModerators.Find(dto.TypeId); if (enMod.UserIdRecipient != userId) { break; } enMod.ConfirmedAt = DateTime.Now; enMod.Status = RequestStatus.Accepted; _context.SaveChanges(); success = true; break; default: Console.WriteLine("Invalid Request Type " + dto.RequestType); break; } if (!success) { return(new OkObjectResult(new { success = false, error = "Bad request type" })); } return(new OkObjectResult(new { success = true })); }
public OkObjectResult NewUser([FromBody] User user) { // Main sign up point for new users if (!ModelState.IsValid) { return(new OkObjectResult(new { success = false, error = "Model Invalid", ModelState })); } var emailInUse = _context.Users.Any(u => u.Email == user.Email); if (emailInUse) { return(new OkObjectResult(new { success = false, error = "Email already in use" })); } if (!BCrypt.Net.BCrypt.Verify(user.PasswordConfirm, user.Password)) { return(new OkObjectResult(new { success = false, error = "Password does not match" })); } _context.Users.Add(user); _context.SaveChanges(); _context.Connections.Add(new Connection { ConfirmedAt = DateTime.Now, Status = RequestStatus.Accepted, UserIdRecipient = user.UserId, UserIdRequester = user.UserId, Text = "New User" }); _context.SaveChanges(); var jwtToken = GenerateJwtToken(new UserDto { UserId = user.UserId }); return(new OkObjectResult(new { success = true, user = new UserDto { UserId = user.UserId, FullName = user.FullName, PhotoUrl = null, Email = user.Email }, jwt = jwtToken })); }