コード例 #1
0
        public async Task <Category> RemoveCategory(Category category)
        {
            _context.Categories.Remove(category);
            await _context.SaveChangesAsync();

            return(category);
        }
コード例 #2
0
        public async Task Create(Account account)
        {
            Validate(account);

            _context.Accounts.Add(account);
            await _context.SaveChangesAsync();
        }
コード例 #3
0
        public async Task <Category> Post([FromBody] Category category)
        {
            _context.Categories.Add(category);
            await _context.SaveChangesAsync();

            return(category);
        }
コード例 #4
0
        public async Task <ProductModel> CreateProduct(ProductModel model)
        {
            var product = new Product();

            model.MapTo(product);
            await _db.Products.AddAsync(product);

            await _db.SaveChangesAsync();

            return(product.MapToDto());
        }
コード例 #5
0
 public async Task AddAsync(Brand brand)
 {
     try
     {
         _context.Brands.Add(brand);
         await _context.SaveChangesAsync();
     }
     catch (Exception ex)
     {
         throw;
     }
 }
コード例 #6
0
 public async Task UpdateCategoryAsync(Category category)
 {
     try
     {
         _context.Entry <Category>(category).State = EntityState.Modified;
         await _context.SaveChangesAsync();
     }
     catch (Exception ex)
     {
         throw new Exception("An error has occurred");
     }
 }
コード例 #7
0
        public async Task DeleteProductAync(Guid id)
        {
            try
            {
                Product obj = await _context.Products.FindAsync(id);

                _context.Entry <Product>(obj).State = EntityState.Deleted;
                await _context.SaveChangesAsync();
            }
            catch (ArgumentNullException ex)
            {
                throw new ArgumentNullException(ex.Message);
            }
        }
コード例 #8
0
        public async Task <IActionResult> Create(Product product)
        {
            db.Products.Add(product);
            await db.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }
コード例 #9
0
        public async Task <bool> Put(Category category)
        {
            bool resultUpdate = false;

            try
            {
                context.Entry <Category>(category).State = EntityState.Modified;
                int resultLine = await context.SaveChangesAsync();

                resultUpdate = resultLine > 0 ? true : false;
            } catch (Exception ex)
            {
                Console.WriteLine("Put -> " + ex.Message);
            }
            return(resultUpdate);
        }
コード例 #10
0
        public override async Task UpdateAsync(Course entity)
        {
            using (StoreDataContext dbContext = new StoreDataContext())
            {
                var course = dbContext.Courses.FirstOrDefault(s => s.CourseId == entity.CourseId);

                course.Name            = entity.Name;
                course.Precentage      = entity.Precentage;
                course.StartedDateTime = entity.StartedDateTime;
                course.EndDateTime     = entity.EndDateTime;
                course.Description     = entity.Description;
                course.Author          = entity.Author;
                course.ModifyDateTime  = DateTime.Now;
                course.CourseStatus    = entity.CourseStatus;

                course.Tags.Clear();

                foreach (var tags in entity.Tags)
                {
                    var dbTag = dbContext.Tags.FirstOrDefault(s => s.TagId == tags.TagId);
                    course.Tags.Add(dbTag);
                }

                await dbContext.SaveChangesAsync();
            }
        }
コード例 #11
0
        public async Task <bool> Save(Product product)
        {
            bool resultSave = false;

            try
            {
                context.Products.Add(product);
                int result = await context.SaveChangesAsync();

                resultSave = result > 0 ? true : false;
            } catch (Exception ex)
            {
                Console.WriteLine("Save -> " + ex.Message);
            }
            return(resultSave);
        }
コード例 #12
0
        public async Task Register(AbstractOperation operation)
        {
            operation.SetDescription();
            operation.CalculateRate();

            _context.AbstractOperation.Add(operation);
            await _context.SaveChangesAsync();
        }
コード例 #13
0
        public async Task <ActionResult> DeleteCategory(
            [FromServices] StoreDataContext _context,
            [FromBody] Category category)
        {
            _context.Categories.Remove(category);
            await _context.SaveChangesAsync();

            return(NoContent());
        }
コード例 #14
0
 public override async Task DeleteAsync(int id)
 {
     using (StoreDataContext dbContext = new StoreDataContext())
     {
         var course = dbContext.Courses.FirstOrDefault(s => s.CourseId == id);
         course.Tags.Clear();
         dbContext.Courses.Remove(course);
         await dbContext.SaveChangesAsync();
     }
 }
コード例 #15
0
        public virtual async Task CreateAsync(T entity)
        {
            using (StoreDataContext dataContext = new StoreDataContext())
            {
                entity.CreatedDateTime = DateTime.Now;
                entity.ModifyDateTime  = DateTime.Now;

                dataContext.Set <T>().Add(entity);
                await dataContext.SaveChangesAsync();
            }
        }
コード例 #16
0
        public async Task <ActionResult> Post(
            [FromServices] StoreDataContext context,
            [FromBody] Product product
            )
        {
            await context.Products.AddAsync(product);

            await context.SaveChangesAsync();

            return(Created("", product));
        }
コード例 #17
0
        public async Task <ActionResult> Post(
            [FromServices] StoreDataContext context,
            [FromBody] Usuario Usuario
            )
        {
            await context.Usuarios.AddAsync(Usuario);

            await context.SaveChangesAsync();

            return(Created("", Usuario));
        }
コード例 #18
0
 public async Task Add(User user)
 {
     try
     {
         _context.Users.Add(user);
         await _context.SaveChangesAsync();
     }
     catch (Exception ex)
     {
         throw;
     }
 }
コード例 #19
0
        // Asenkron olarak çalışabilen ve sayfadaki Submit işlemi sonrası tetiklenen Post metodumuz
        // Tipik olarak Razor sayfasındaki model verisini alıp DbSet'e ekliyor ve kayıt ediyoruz.
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var addedBook = _context.Books.Add(BookData).Entity;

            Console.WriteLine($"{addedBook.Title} eklendi");
            await _context.SaveChangesAsync();

            return(RedirectToPage("/Index")); // Kitap eklendikten sonra ana sayfaya yönlendirme yapıyoruz
        }
コード例 #20
0
        public async Task <ActionResult> PutCategory(
            [FromServices] StoreDataContext _context,
            [FromBody] Category category)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.Entry <Category>(category).State = EntityState.Modified;
            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(PutCategory), category));
        }
コード例 #21
0
        // Silme operasyonunu icra eden metodumuz
        public async Task <IActionResult> OnPostDeleteAsync(int id)
        {
            // Silme operasyonu için Identity alanından önce
            // kitabı bul
            var book = await _context.Books.FindAsync(id);

            if (book != null) //Kitabı bulduysan
            {
                _context.Books.Remove(book);
                //Kitabı çıkart ve Context'i son haliyle kaydet
                await _context.SaveChangesAsync();
            }
            return(RedirectToPage()); // Scotty bizi o anki sayfaya döndür
        }
コード例 #22
0
        public async Task <ResultViewModel> Post([FromBody] EditorProductViewModel model)
        {
            model.Validate();

            if (model.IsValid)
            {
                return new ResultViewModel {
                           Success = false,
                           Message = "Não foi possível cadastrar o produto",
                           Data    = model.Notifications
                }
            }

            var product = new Product()
            {
                Title          = model.Title,
                CategoryId     = model.CategoryId,
                CreateDate     = DateTime.Now,
                Description    = model.Description,
                Image          = model.Image,
                LastUpdateDate = DateTime.Now,
                Price          = model.Price,
                Quantity       = model.Quantity
            };

            _context.Products.Add(product);

            await _context.SaveChangesAsync();


            return(await Task.FromResult(new ResultViewModel()
            {
                Success = true,
                Message = "Produto cadastrado com sucesso",
                Data = product
            }));
        }
コード例 #23
0
        public async Task <ActionResult> PostCategory(
            [FromServices] StoreDataContext _context,
            [FromBody] Category category)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            await _context.Categories.AddAsync(category);

            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(PostCategory), category));
        }
コード例 #24
0
        public async Task <ActionResult> Delete(
            [FromServices] StoreDataContext context,
            int id
            )
        {
            var item = await context.Products.FirstOrDefaultAsync(x => x.Id == id);

            if (item == null)
            {
                return(NotFound());
            }

            context.Products.Remove(item);
            await context.SaveChangesAsync();

            return(Ok(item));
        }
コード例 #25
0
        public override async Task CreateAsync(Course entity)
        {
            using (StoreDataContext dbContext = new StoreDataContext())
            {
                foreach (var tags in entity.Tags)
                {
                    dbContext.Entry(tags).State = EntityState.Unchanged;
                    dbContext.Tags.Attach(tags);
                }

                entity.CreatedDateTime = DateTime.Now;
                entity.ModifyDateTime  = DateTime.Now;

                dbContext.Courses.Add(entity);

                await dbContext.SaveChangesAsync();
            }
        }
コード例 #26
0
        public async Task <ActionResult> Put(
            [FromServices] StoreDataContext context,
            [FromBody] Product product,
            int id
            )
        {
            var item = await context.Products.FirstOrDefaultAsync(x => x.Id == id);

            if (item == null)
            {
                return(NotFound());
            }

            context.Entry <Product>(product).State = EntityState.Modified;
            await context.SaveChangesAsync();

            return(Ok(item));
        }
コード例 #27
0
        public async Task <OrderRowModel> AddRowToOrder(long id, OrderRowModel model)
        {
            if (await OrderExists(id) == false)
            {
                throw new EntityNotFoundException <OrderModel>(id);
            }

            var row = new OrderRow();

            model.MapTo(row);

            row.OrderId = id;
            await _db.OrderRows.AddAsync(row);

            await _db.SaveChangesAsync();

            return(row.MapToDto());
        }
コード例 #28
0
        public async Task <IActionResult> OnPostAsync()
        {
            // Eksik veya hatalı bilgiler nedeniyle Model örneği doğrulanamadıysa
            // sayfada kalalım
            if (!ModelState.IsValid)
            {
                return(Page());
            }
            // Güncellenen kitap bilgilerini Context'e ilave edip durumunu Modified'e çektik
            _context.Attach(BookData).State = EntityState.Modified;

            try
            {
                // Değişiklikleri kaydetmeyi deniyoruz
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                throw new Exception($"{BookData.Id} numaralı kitabı bulamadık!");
            }

            // İşlemler başarılı ise tekrardan index'e(Anasayfa oluyor tabii) dönüyoruz
            return(RedirectToPage("/index"));
        }
コード例 #29
0
        public async Task Save(Product product)
        {
            await _context.Products.AddAsync(product);

            await _context.SaveChangesAsync();
        }
コード例 #30
0
        public virtual async Task Save(TType entity)
        {
            await _context.Set <TType>().AddAsync(entity);

            await _context.SaveChangesAsync();
        }