public IActionResult InsertarEstudiante(Estudiante estudiante) { if (!ModelState.IsValid) { return(BadRequest(new { Error = "Datos no vàlidos" })); } Transaccion transaccion = new Transaccion(); transaccion.Fecha = DateTime.Now; transaccion.Hora = DateTime.Now; transaccion.ip = Request.HttpContext.Connection.RemoteIpAddress.ToString(); try{ _contextoMySQL.estudiante.Add(estudiante); _contextoMySQL.SaveChanges(); transaccion.Aprobado = true; } catch { transaccion.Aprobado = false; } finally{ _contextoSqlite.transaccion.Add(transaccion); _contextoSqlite.SaveChanges(); } return(RedirectToAction("ListarEstudiantes")); }
static void Delete() { int id = 1; using (var db = new DALContext()) { db.IceCreams.Add(new IceCream { Name = "A supprimer" }); db.SaveChanges(); foreach (var iceC in db.IceCreams) { Console.WriteLine($"Avant\nName: {iceC.Name}"); } Console.WriteLine("*****"); var icetod = db.IceCreams.Where(x => x.ID == id).FirstOrDefault(); Console.WriteLine($"{icetod.ID} {icetod.Name}"); db.IceCreams.Remove(icetod); db.SaveChanges(); foreach (var iceC in db.IceCreams) { Console.WriteLine($"Après\nName: {iceC.Name}"); } } }
public IActionResult InsertarEstudiante([FromBody] Estudiante estudiante) { if (!ModelState.IsValid) { BadRequest(); } var transaccion = new Transaccion() { Fecha = DateTime.Now, Hora = DateTime.Now, ip = Request.HttpContext.Connection.RemoteIpAddress.ToString() }; try{ _contextoMySQL.estudiante.Add(estudiante); _contextoMySQL.SaveChanges(); transaccion.Aprobado = true; } catch { transaccion.Aprobado = false; } finally{ _contextoSqlite.transaccion.Add(transaccion); _contextoSqlite.SaveChanges(); } Response.StatusCode = 201; return(CreatedAtRoute("GetEstudianteByName", new { nombre = estudiante.Nombre }, estudiante)); }
public ActionResult Create([Bind(Include = "ID,Name,ParentRegionID")] ListingRegion listingRegion) { if (ModelState.IsValid) { db.ListingRegions.Add(listingRegion); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.ParentRegionID = new SelectList(db.ListingRegions, "ID", "Name", listingRegion.ParentRegionID); return(View(listingRegion)); }
public ActionResult Create([Bind(Include = "ID,Title,Description,Address,Phone1,Phone2,Website,Lat,Long,VideoUrl,FacebookUrl,TwitterUrl,YoutubeUrl,PinterestUrl,SubmittedBy,ConfirmedBy,SubmitDate,ConfirmDate,CategoryID,ListingRegionID")] Venue venue) { if (ModelState.IsValid) { db.Venues.Add(venue); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.CategoryID = new SelectList(db.Categories, "ID", "Name", venue.CategoryID); ViewBag.ListingRegionID = new SelectList(db.ListingRegions, "ID", "Name", venue.ListingRegionID); return(View(venue)); }
public string DeleteProduct(string name) { var product = _dalContext.GetProducts.Single(x => x.Name == name); try { _dalContext.RemoveProduct(product); _dalContext.SaveChanges(); return(success); } catch { return(failure); } }
private async Task <bool> AddLogin(DALContext context, string password, string confirmationKey) { try { if (context.SearchForUserByEmailAddress(EmailAddress) != null) { return(false); } int RoleId = context.Roles.FirstOrDefault(x => x.Rolename == "user").RoleId; // add login var user = new Models.Account.LoginInfo() { UserId = Guid.NewGuid().ToString(), EmailAddress = EmailAddress, Password = password, FirstName = FirstName, LastName = LastName, EmailConfirmationKey = confirmationKey, EmailConfirmed = false, RoleId = RoleId, }; context.Users.Add(user); context.SaveChanges(); return(true); } catch { return(false); } }
public bool CreateLoginForUser(UserDto user) { var existingUser = _dalContext.GetUserByNameAndEmail(user.Name, user.Email); var successful = UserPassesAgeCheck(user); if (existingUser == null) { user.Logins.Single().Successful = successful; _dalContext.Users.Add(UserFactory.CreateUser(user)); } else { var login = LoginFactory.CreateLogin(user.Logins.Single()); login.User = existingUser; login.Successful = successful; _dalContext.Logins.Add(login); } _dalContext.SaveChanges(); if (successful) { return(true); } return(false); }
public ActionResult Create([Bind(Include = "ID,Name")] Category category) { try { if (ModelState.IsValid) { db.Categories.Add(category); db.SaveChanges(); return(RedirectToAction("Index")); } } catch (Exception ex) { logger.Error(ex, "CategoryController::Create"); } return(View(category)); }
public void Dispose() { var loginSaved = _context.Logins.Single(x => x.User.Name == user); _context.Logins.Remove(loginSaved); var userSaved = _context.Users.Single(x => x.Name == user); _context.Users.Remove(userSaved); _context.SaveChanges(); }
static void Main(string[] args) { using (var ctx = new DALContext()) { ctx.Users.Add(new User() { Name = "John", Phone = "123-132-1232", Email = "*****@*****.**", BirthDate = DateTime.Today, JoinDate = DateTime.Today, Id = 1 }); ctx.SaveChanges(); Console.ReadKey(); } }
public void GetCityByCEPTest() { var context = new DALContext(); var StatesList = new List <State>(); StatesList.Add(new State() { Name = "RONDÔNIA", UF = "RO", IBGECode = 11, CountryId = 1 }); StatesList.Add(new State() { Name = "ACRE", UF = "AC", IBGECode = 12, CountryId = 1 }); StatesList.Add(new State() { Name = "AMAZONAS", UF = "AM", IBGECode = 13, CountryId = 1 }); StatesList.Add(new State() { Name = "RORAIMA", UF = "RR", IBGECode = 14, CountryId = 1 }); StatesList.Add(new State() { Name = "PARÁ", UF = "PA", IBGECode = 15, CountryId = 1 }); StatesList.Add(new State() { Name = "AMAPÁ", UF = "AP", IBGECode = 16, CountryId = 1 }); StatesList.Add(new State() { Name = "TOCANTINS", UF = "TO", IBGECode = 17, CountryId = 1 }); StatesList.Add(new State() { Name = "MARANHÃO", UF = "MA", IBGECode = 21, CountryId = 1 }); StatesList.Add(new State() { Name = "PIAUÍ", UF = "PI", IBGECode = 22, CountryId = 1 }); StatesList.Add(new State() { Name = "CEARÁ", UF = "CE", IBGECode = 23, CountryId = 1 }); StatesList.Add(new State() { Name = "RIO GRANDE DO NORTE", UF = "RN", IBGECode = 24, CountryId = 1 }); StatesList.Add(new State() { Name = "PARAÍBA", UF = "PB", IBGECode = 25, CountryId = 1 }); StatesList.Add(new State() { Name = "PERNAMBUCO", UF = "PE", IBGECode = 26, CountryId = 1 }); StatesList.Add(new State() { Name = "ALAGOAS", UF = "AL", IBGECode = 27, CountryId = 1 }); StatesList.Add(new State() { Name = "SERGIPE", UF = "SE", IBGECode = 28, CountryId = 1 }); StatesList.Add(new State() { Name = "BAHIA", UF = "BA", IBGECode = 29, CountryId = 1 }); StatesList.Add(new State() { Name = "MINAS GERAIS", UF = "MG", IBGECode = 31, CountryId = 1 }); StatesList.Add(new State() { Name = "ESPÍRITO SANTO", UF = "ES", IBGECode = 32, CountryId = 1 }); StatesList.Add(new State() { Name = "RIO DE JANEIRO", UF = "RJ", IBGECode = 33, CountryId = 1 }); StatesList.Add(new State() { Name = "SÃO PAULO", UF = "SP", IBGECode = 35, CountryId = 1 }); StatesList.Add(new State() { Name = "PARANÁ", UF = "PR", IBGECode = 41, CountryId = 1 }); StatesList.Add(new State() { Name = "SANTA CATARINA", UF = "SC", IBGECode = 42, CountryId = 1 }); StatesList.Add(new State() { Name = "RIO GRANDE DO SUL", UF = "RS", IBGECode = 43, CountryId = 1 }); StatesList.Add(new State() { Name = "MATO GROSSO DO SUL", UF = "MS", IBGECode = 50, CountryId = 1 }); StatesList.Add(new State() { Name = "MATO GROSSO", UF = "MT", IBGECode = 51, CountryId = 1 }); StatesList.Add(new State() { Name = "GOIÁS", UF = "GO", IBGECode = 52, CountryId = 1 }); StatesList.Add(new State() { Name = "DISTRITO FEDERAL", UF = "DF", IBGECode = 53, CountryId = 1 }); StatesList.ForEach(p => { context.States.Create(p); }); context.SaveChanges(); Assert.Fail(); }
public IActionResult CreateAlbum(ICollection <Microsoft.AspNet.Http.IFormFile> Productphoto_file, Album model) { try { // ResultBundle result = ResultBundle.Failed(); if (ModelState.IsValid) { if (Productphoto_file.Count > 0) { foreach (var file in Productphoto_file) { if (file.Length > 1024 * 1024) { TempData["ErrorMessage"] = "حجم فایل انتخاب شده بیش از یک مگابایت است. لطفا فایل دیگری انتخاب نمایید"; break; } else if (file.Length == 0) { TempData["ErrorMessage"] = "حجم فایل انتخاب شده صفر بایت است. لطفا فایل دیگری انتخاب نمایید"; break; } else { var fileName = Microsoft.Net.Http.Headers.ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); using (Stream sr = file.OpenReadStream()) { byte[] fileData = null; using (MemoryStream msOrig = Utils.LoadToMemoryStream(sr)) { // resize it // todo: we always resize profile image to 600x500 Image img = Bitmap.FromStream(msOrig); // Bitmap bmp = new Bitmap(img, new Size(600, 500)); Bitmap bmp = new Bitmap(img, new Size(340, 280)); MemoryStream ms = new MemoryStream(); bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); // read content instead fileData = Utils.ConvertMemoryStreamToBytes(ms); ms.Dispose(); bmp.Dispose(); img.Dispose(); } // _db = new DALContext(); Models.Album Album = new Models.Album(); Album.AssetData = new Byte[fileData.Length]; Album.AssetType = "jpg"; Buffer.BlockCopy(fileData, 0, Album.AssetData, 0, fileData.Length); //imgc.Id = 2; ViewBag.imgusedatabaseController = Album.AssetData;//for img usedatabaseController Album.Titel = model.Titel; _db.Albums.Add(Album); _db.SaveChanges(); TempData["ErrorMessage"] = ("درج با موفقیت انجام شد"); } } } } else { TempData["ErrorMessage"] = ("لطفا یک عکس انتخاب نمایید "); // result.AddMessage("لطفا یک عکس انتخاب نمایید "); } } else { TempData["ErrorMessage"] = ("لطفا یک عکس انتخاب کنید و عنوان را وارد نمایید "); // result.AddMessage("لطفا یک عکس انتخاب کنید و عنوان را وارد نمایید "); } // ViewBag.errorMessage = result.FormattedMessages; return(View()); } catch (Exception ex) { return(RedirectToAction(nameof(HomeController.Error), "Home")); } }
public void Post(Product product) { ctx.Products.Add(product); ctx.SaveChanges(); }
// POST api/values public void Post([FromBody] User user) { ctx.Users.Add(user); ctx.SaveChanges(); }
public void GetAllLoginsByUser_ReturnsLoginsForUser() { var options = new DbContextOptionsBuilder <DALContext>() .UseInMemoryDatabase(databaseName: "GetAllLoginsByUser") .Options; var doB = new DateTime(2019, 01, 01); var userNameToSearch = "Name"; var listOfUserLogins = new List <DateTime> { new DateTime(2019, 01, 01), new DateTime(2019, 01, 02), new DateTime(2019, 01, 03) }; var notUserListOfLogins = new List <DateTime> { new DateTime(2019, 01, 04), new DateTime(2019, 01, 05) }; var userDto = new UserDto { Name = userNameToSearch, DateOfBirth = doB }; var user = new User { Name = userNameToSearch, DateOfBirth = doB }; var anotherUser = new User { Name = "Not this", DateOfBirth = doB }; var allLogins = new List <Login>(); foreach (var loginTime in listOfUserLogins) { allLogins.Add(new Login { DateTime = loginTime, User = user }); } foreach (var loginTime in notUserListOfLogins) { allLogins.Add(new Login { DateTime = loginTime, User = anotherUser }); } using (var context = new DALContext(options)) { context.Users.Add(user); context.Users.Add(anotherUser); foreach (var login in allLogins) { context.Logins.Add(login); } context.SaveChanges(); } using (var context = new DALContext(options)) { var service = new UserService(context); var result = service.GetAllLoginsByUser(userDto.Name, userDto.Email); Assert.Equal(3, result.Count()); } }
public void GetAllProductsByCategory_ReturnsProductsForCategory() { var options = new DbContextOptionsBuilder <DALContext>() .UseInMemoryDatabase(databaseName: "GetAllProductsByCategory") .Options; var name = "new product"; var categoryToSearch = "Fruit"; var listOfFruits = new List <string> { "Strawberries", "Bananas", "Oranges" }; var listOfNotFruits = new List <string> { "Bear", "Window" }; var categorySearched = new Category { Name = categoryToSearch }; var notCategorySearched = new Category { Name = "Not this" }; var allProducts = new List <Product>(); foreach (var fruit in listOfFruits) { allProducts.Add(new Product { Name = fruit, Description = fruit, Category = categorySearched }); } foreach (var notFruit in listOfNotFruits) { allProducts.Add(new Product { Name = notFruit, Description = notFruit, Category = notCategorySearched }); } // Insert seed data into the database using one instance of the context using (var context = new DALContext(options)) { foreach (var product in allProducts) { context.Products.Add(product); } context.SaveChanges(); } // Use a clean instance of the context to run the test using (var context = new DALContext(options)) { var service = new ProductService(context); var result = service.GetAllProductsByCategory(categoryToSearch); Assert.Equal(3, result.Count()); } }