public IActionResult Registro([FromBody] Usuario usuario) { var userWithSameEmail = _dashboardDbContext.Usuarios.Where(u => u.Email == usuario.Email).SingleOrDefault(); if (userWithSameEmail != null) { return(BadRequest("Um usuário com o mesmo email já existe")); } if (usuario.Senha != usuario.ConfirmarSenha) { return(BadRequest("Senhas não são iguais.")); } var usuarioObj = new Usuario() { Nome = usuario.Nome, Sobrenome = usuario.Sobrenome, Telefone = usuario.Telefone, Endereco = usuario.Endereco, Email = usuario.Email, DataNascimento = usuario.DataNascimento, DataInclusao = DateTime.Now, Senha = SecurePasswordHasherHelper.Hash(usuario.Senha), ConfirmarSenha = SecurePasswordHasherHelper.Hash(usuario.ConfirmarSenha), Status = '1' }; _dashboardDbContext.Usuarios.Add(usuarioObj); _dashboardDbContext.SaveChanges(); return(StatusCode(StatusCodes.Status201Created)); }
public ActionResult TakePart(string id) { if (@Session["UserMail"] == null) { return(RedirectToAction("Index", "Home")); } if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } ViewBag.Id = id; string userMail = Session["UserMail"].ToString(); var loggedUser = db.Users.Where(registeredUser => registeredUser.Mail.Equals(userMail)).FirstOrDefault(); Tournament tournament = db.Tournaments.Find(id); tournament.RegisteredUsers.Add(loggedUser); db.Tournaments.Attach(tournament); //db.Entry(tournament).State = EntityState.Modified; db.SaveChanges(); //loggedUser.registeredTournaments.Add(tournament); //db.Users.Attach(loggedUser); //db.SaveChanges(); //db.Entry(loggedUser).State = EntityState.Modified; //db.SaveChanges(); return(View()); }
public static bool Initialize(DashboardDbContext context, IOptions <MainUserData> options) { context.Database.EnsureCreated(); if (string.IsNullOrWhiteSpace(options.Value.Email) || string.IsNullOrWhiteSpace(options.Value.Password)) { return(false); } var email = options.Value.Email; var password = options.Value.Password; var name = options.Value.Name ?? ""; var surname = options.Value.Surname ?? ""; if (!IsValidEmail(email)) { return(false); } var userInDb = context.Users.Where(u => u.Email == email).FirstOrDefault(); if (userInDb == null) { var user = new User() { Email = email, Name = name, Surname = surname, Password = new HashService().Hash(password), IsActive = true, isPermanent = true, Claims = new string[] { ClaimType.isAdmin.ToString() } }; try { context.Add(user); context.SaveChanges(); return(true); } catch (Exception) { return(false); } } userInDb.Password = new HashService().Hash(password); userInDb.Name = name; userInDb.Surname = surname; try { context.SaveChanges(); return(true); } catch (Exception) { return(false); } }
public ActionResult ForgotPassword([Bind(Include = "Id, Mail, ForgotPasswordKey, Password")] User user) { if (@Session["UserMail"] == null) { return(RedirectToAction("Index", "Home")); } if (ModelState.IsValid) { var forgetfulUser = db.Users.Where(registeredUser => registeredUser.Mail.Equals(user.Mail) && registeredUser.ForgotPasswordKey.Equals(user.ForgotPasswordKey)).FirstOrDefault(); if (forgetfulUser != null) //good mail and forgotPasswordKey { forgetfulUser.Password = user.Password; db.Entry(forgetfulUser).State = EntityState.Modified; db.SaveChanges(); Response.Write("<script>alert('Your password have been changed');</script>"); return(RedirectToAction("Login")); } else { Response.Write("<script>alert('Incorrect e-mail addres or forgotPassword key');</script>"); return(View()); } } return(View(user)); }
public IActionResult Post(Vehicle vehicle) { var userEmail = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email).Value; var user = _dashboardDbContext.User.FirstOrDefault(u => u.Email == userEmail); if (user == null) { return(NotFound()); } var vehicleObj = new Vehicle() { Title = vehicle.Title, Description = vehicle.Description, Color = vehicle.Color, Company = vehicle.Company, Condition = vehicle.Condition, DatePosted = vehicle.DatePosted, Engine = vehicle.Engine, Price = vehicle.Price, Model = vehicle.Model, Location = vehicle.Location, CategoryId = vehicle.CategoryId, IsFeatured = false, IsHotAndNew = false, UserId = user.Id }; _dashboardDbContext.Vehicles.Add(vehicleObj); _dashboardDbContext.SaveChanges(); return(Ok(new { vehicleId = vehicleObj.Id, message = "Veiculo adicionado com sucesso" })); }
public IActionResult Post([FromBody] Image imageModel) { var userEmail = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email).Value; var user = _dashboardDbContext.User.FirstOrDefault(u => u.Email == userEmail); if (user == null) { return(NotFound()); } var stream = new MemoryStream(imageModel.ImageArray); var guid = Guid.NewGuid().ToString(); var file = $"{guid}.jpg"; var folder = "wwwroot"; var response = FilesHelper.UploadImage(stream, folder, file); if (!response) { return(BadRequest()); } else { var image = new Image() { ImageUrl = file, VehicleId = imageModel.VehicleId }; _dashboardDbContext.Images.Add(image); _dashboardDbContext.SaveChanges(); return(StatusCode(StatusCodes.Status201Created)); } }
public IActionResult Register([FromBody] User user) { var userWithSameEmail = _dashboardDbContext.User.Where(u => u.Email == user.Email).SingleOrDefault(); if (userWithSameEmail != null) { return(BadRequest("Um usuário com o mesmo email já existe")); } var userObj = new User() { Name = user.Name, Email = user.Email, Password = SecurePasswordHasherHelper.Hash(user.Password), }; _dashboardDbContext.User.Add(userObj); _dashboardDbContext.SaveChanges(); return(StatusCode(StatusCodes.Status201Created)); }
/// <summary> /// Write bad response to DB and logic /// </summary> /// <param name="response">Portal response</param> /// <param name="id">Portal Id</param> /// <returns></returns> private async Task WriteBadResponseAsync(PortalResponse response, Guid id) { var portal = await _context.Portals.FindAsync(id); portal.PortalResponses.Add(response); _context.SaveChanges(); await WriteLastResponseData(portal, response); // Portal with responses history and email notification sending var portalWithResponses = _context.Portals.Include(r => r.PortalResponses).First(p => p.Id == id); await _notificationsService.SendNotificationEmailAsync(portalWithResponses, response); }
public ActionResult Register([Bind(Include = "Id,Mail,Password")] User user) { if (@Session["UserMail"] == null) { return(RedirectToAction("Index", "Home")); } if (ModelState.IsValid) { if (db.Users.Any(randomUser => randomUser.Mail == user.Mail)) { Response.Write("<script>alert('Account with this e-mail addres already exists');</script>"); return(View()); } db.Users.Add(user); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(user)); }
/// <summary> /// Sets LastNotificationSent property to given DateTime /// </summary> /// <param name="Id">Portal Id</param> /// <param name="dateTime">DateTime to set</param> /// <returns>Portal updated</returns> public async Task <Portal> SetLastNotificationSentAsync(Guid Id, DateTime dateTime) { try { var portal = await _context.Portals.FindAsync(Id); if (portal != null) { portal.LastNotificationSent = dateTime; _context.SaveChanges(); _logger.LogInformation("Portal (Id: {0}) last notification sent datetime changed.", portal.Id); return(portal); } _logger.LogWarning("Portal (Id: {0}) not found for changing last notification sent datetime.", portal.Id); return(null); } catch (Exception ex) { _logger.LogError(ex.Message); return(null); } }