public async Task <IActionResult> Property(UserProperty property, IFormFile file) { try { property.Username = ViewData["Username"].ToString(); // convert image to be stored in DB if (file != null) { Log.Information("Convert image to format for DB"); using (var ms = new MemoryStream()) { file.CopyTo(ms); property.ImageUrl = ms.ToArray(); } } // If property exist, update to DB if (property.PropertyId > 0) { Log.Information("Updating property"); _context.Update(property); await _context.SaveChangesAsync(); } else { Log.Information("Adding property"); _context.Add(property); await _context.SaveChangesAsync(); } // check newly added property and assign property value to user db Log.Information("Updating added property value to user db"); var prop = _context.UserProperty.FirstOrDefault(x => x.Username.Equals(ViewData["Username"].ToString())); var user = _context.User.FirstOrDefault(x => x.Username.Equals(ViewData["Username"].ToString())); user.PropertyId = prop.PropertyId; _context.Update(user); await _context.SaveChangesAsync(); Log.Information("Success to adding/updating property information"); TempData["Success"] = "Success"; return(RedirectToAction("Account", "Home")); } catch (Exception ex) { Log.ForContext <UserController>().Error(ex, "Error to adding/updating property information"); TempData["Error"] = "Error with server, please contact customer support"; return(View(property)); } }
public async Task <IActionResult> Register(User user, UserProperty userProperty, IFormFile file, Payment payment) { Log.Information("Registering user..."); // run if modal is valid using data annotation built in model try { // set role to User, admin can only be added through SQL user.Role = "User"; _context.Add(user); await _context.SaveChangesAsync(); if (payment.Number != null) { _context.Add(payment); await _context.SaveChangesAsync(); } if (userProperty.PropAddress != null) { if (file != null) { Log.Information("Convert image to format for DB"); using (var ms = new MemoryStream()) { file.CopyTo(ms); userProperty.ImageUrl = ms.ToArray(); } } _context.Add(userProperty); await _context.SaveChangesAsync(); } user.PropertyId = userProperty.PropertyId; user.PaymentId = payment.PaymentId; _context.Update(user); await _context.SaveChangesAsync(); TempData["Success"] = "Account successfully created!"; return(RedirectToAction(nameof(Index))); } catch (Exception ex) { Log.ForContext <HomeController>().Error(ex, "Problem with registration for {0}", user.Username); } return(View(user)); }
public async Task <IActionResult> Add(Parking parking, IFormFile file) { try { // convert image to be stored in DB if (file != null) { Log.Information("Convert image to format for DB"); using (var ms = new MemoryStream()) { file.CopyTo(ms); parking.Image = ms.ToArray(); } } if (parking.ParkingId > 0) { Log.Information("Updating parking"); _context.Update(parking); await _context.SaveChangesAsync(); } else { Log.Information("Adding parking"); _context.Add(parking); await _context.SaveChangesAsync(); } Log.Information("Successfully added parking"); TempData["Success"] = "Success"; return(RedirectToAction("Parking")); } catch (Exception ex) { Log.ForContext <AdminController>().Error(ex, "Error in adding parking"); TempData["Error"] = "Error with server, please contact customer support"; return(View(parking)); } }