Exemple #1
0
        public async Task <ActionResult <LocalModel> > PostLocal(LocalModel item)
        {
            _context.Local.Add(item);
            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetLocal), new { id = item.Id }, item));
        }
Exemple #2
0
        /// <summary>
        /// Copies unprocessed messages from local database to remote.
        /// If copied successfully, source's property IsProcessed = true.
        /// </summary>
        /// <param name="ct"></param>
        /// <returns></returns>
        public async Task CopyNewLocalMessagesToRemote(CancellationToken ct = default)
        {
            var localMessages = _localContext.Messages.Where(x => x.IsProcessed == false).ToList();

            if (localMessages.Count == 0)
            {
                return;
            }
            // Clone messages
            var clonedMessages = new List <Message>();

            localMessages.ForEach(m => clonedMessages.Add(m.Clone()));
            // Add cloned messages to remote context and save.
            // Because local database does not support column type of C# DateTimeOffset,
            // before insert to Azure MSSql, CreatedOn is retrieved from MessageBody field.
            clonedMessages.ForEach(x =>
            {
                x.Id            = 0;
                var messageBody = JsonConvert.DeserializeAnonymousType(x.MessageBody, new { CreatedOn = "" });
                x.CreatedOn     = DateTime.Parse(messageBody.CreatedOn);
            });
            await _remoteContext.Messages.AddRangeAsync(clonedMessages, ct);

            //remoteContext.Messages.AddRange(clonedMessages);
            var savedClonedMessagesCount = 0;

            try
            {
                savedClonedMessagesCount = await _remoteContext.SaveChangesAsync(ct);
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Database error");
                throw;
            }
            //var savedClonedMessagesCount = remoteContext.SaveChanges();
            // If copied succesfully, mark local as processed.
            if (savedClonedMessagesCount == clonedMessages.Count)
            {
                localMessages.ForEach(x => x.IsProcessed = true);
                _localContext.UpdateRange(localMessages);
                try
                {
                    await _localContext.SaveChangesAsync(ct);
                }
                catch (Exception e) { _logger.LogError(e, "Database error while copying messages to remote."); }
            }

            _logger.LogDebug("New Message records added to Azure: {savedClonedMessagesCount}", savedClonedMessagesCount);
            return;
        }
        public virtual async Task <T> AddAsync(T t)
        {
            _context.Set <T>().Add(t);
            await _context.SaveChangesAsync();

            return(t);
        }
Exemple #4
0
        public async Task <ActionResult> Create([Bind(Include = "standard_id,standard_name")] Standard standard)
        {
            if (ModelState.IsValid)
            {
                ViewBag.Error = null;
                if (StandardExists(standard.standard_name))
                {
                    ViewBag.Error = "Standard Name Already Exists!";
                    return(View());
                }
                db.standard.Add(standard);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(View(standard));
        }
Exemple #5
0
        public async Task <ActionResult> Create([Bind(Include = "student_id,student_name,standard_id")] Student student)
        {
            if (ModelState.IsValid)
            {
                ViewBag.Error = null;
                if (StudentExists(student.student_name, Convert.ToInt32(student.standard_id)))
                {
                    ViewBag.Error = "Student Name Already Exists in the selected Standard!";
                    return(View());
                }
                db.student.Add(student);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            ViewBag.standard_id = new SelectList(db.standard, "standard_id", "standard_name", student.standard_id);
            return(View(student));
        }
        public async Task <IActionResult> TestSucceed(int Identifier)
        {
            if (Identifier == null)
            {
                return(NotFound());
            }

            var endpointDevice = await _context.EndpointDevices
                                 .FirstOrDefaultAsync(m => m.Identifier == Identifier);

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

            endpointDevice.Tested     = Tested.Tak;
            endpointDevice.TestResult = TestResult.Pozytywny;

            if (await _context.SaveChangesAsync() > 0)
            {
                return(RedirectToAction("Index", new RouteValueDictionary(new { action = "Index", id = Identifier })));
            }

            return(BadRequest("Coś poszło nie tak."));
        }
        public async void HookedDbContext_MustCallHooks_WhenRunningSaveChangesAsync()
        {
            var hooks = new IHook[]
                            {
                                new TimestampPreInsertHook()
                            };

            var context = new LocalContext(hooks);
            var entity = new TimestampedSoftDeletedEntity();
            context.Entities.Add(entity);
            await context.SaveChangesAsync();

            Assert.AreEqual(entity.CreatedAt.Date, DateTime.Today);
        }