public void Update(Users userParam, string pass) { var userUpd = _context.Users.Find(userParam.IdUsers); if (userUpd == null) { throw new AppException("User not found!"); } if (userParam.Email != userUpd.Email) { if (_context.Users.Any(x => x.Email == userParam.Email)) { throw new AppException("Email " + userParam + " is already"); } } userUpd.FirstName = userParam.FirstName; userUpd.LastName = userParam.LastName; userUpd.SecondName = userParam.SecondName; userUpd.Telephone = userParam.Telephone; userUpd.PositionId = userParam.PositionId; userUpd.RulesId = userParam.RulesId; userUpd.CompanyId = userParam.CompanyId; if (!string.IsNullOrWhiteSpace(pass)) { byte[] passHash, passSalt; CreatePasswordHash(pass, out passHash, out passSalt); userUpd.PassHash = passHash; userUpd.PassSalt = passSalt; } _context.Users.Update(userUpd); _context.SaveChanges(); }
public Users Create(Users newUser, string pass) { newUser.RulesId = 1; newUser.PositionId = 1; Console.WriteLine("Create user!"); if (string.IsNullOrWhiteSpace(pass)) { throw new AppException("Password is required"); } using (servicedbContext db = new servicedbContext()) { if (_context.Users.Any(x => x.Email == newUser.Email)) { throw new ApplicationException("Username " + newUser.Email + " is already taken"); } } byte[] passHash, passSalt; CreatePasswordHash(pass, out passHash, out passSalt); newUser.PassHash = passHash; newUser.PassSalt = passSalt; using (servicedbContext db = new servicedbContext()){ db.Users.Add(newUser); db.SaveChanges(); } return(newUser); }
public async Task <IActionResult> UploadFile(IFormFile image) { var images = Request.Form.Files; if (images == null || images.Count == 0) { return(Content("image not selected")); } foreach (var item in images) { var path = Path.Combine( Directory.GetCurrentDirectory(), "files", item.FileName); using (var stream = new FileStream(path, FileMode.Create)) { await item.CopyToAsync(stream); } Images newImage = new Images(); newImage.Name = item.Name; newImage.Path = path; using (servicedbContext db = new servicedbContext()) { db.Images.Add(newImage); db.SaveChanges(); } } return(RedirectToAction("LastImage")); }
public IActionResult PostOrders([FromBody] OrderDto orderDto) { Console.WriteLine("Post order:"); if (ModelState.IsValid) { Orders neworder = new Orders(); neworder.Caption = orderDto.Caption.Trim(); neworder.Description = orderDto.Description.Trim(); neworder.Cost = orderDto.Cost; neworder.Url = DateTime.Now.ToString(); neworder.UpTo = orderDto.UpTo; neworder.UsersId = 1; using (servicedbContext db = new servicedbContext()) { Console.WriteLine("Post order: " + neworder.Caption.ToString()); db.Orders.Add(neworder); Companyorders newRelation = new Companyorders(); newRelation.IdCompanies = orderDto.CompanyId; newRelation.IdOrders = neworder.IdOrders; db.Companyorders.Add(newRelation); db.SaveChanges(); Console.WriteLine("Post response order: " + neworder.Caption.ToString()); return(Ok(neworder)); } } else { Console.WriteLine(ModelState.ValidationState); return(BadRequest(ModelState.ValidationState)); } }
public Companies Create(Companies company) { using (servicedbContext db = new servicedbContext()) { db.Companies.Add(company); db.SaveChanges(); } return(company); }
public IActionResult Create([FromBody] AddressDto address) { if (ModelState.IsValid) { Addresses newAddr = new Addresses(); newAddr.Street = address.Street; newAddr.Number = address.Number; newAddr.Flat = address.Flat; newAddr.CityId = 1; using (servicedbContext db = new servicedbContext()) { db.Addresses.Add(newAddr); db.SaveChanges(); } return(Ok(new{ IdAddress = newAddr.IdAddress })); } else { return(BadRequest(ModelState.ValidationState)); } }