Ejemplo n.º 1
0
        private async Task <object?> RemoveEndpoint(string userId, IReadOnlyDictionary <string, string?> parameters,
                                                    CancellationToken cancellationToken)
        {
            if (!parameters.TryGetValue("mainParam", out var endpointKey) || string.IsNullOrWhiteSpace(endpointKey))
            {
                return new { ResultType = SubmitClientResultType.OkWithMessage, ResultValue = "No endpoint to delete" }
            }
            ;

            if (endpointKey.Equals("test", StringComparison.OrdinalIgnoreCase))
            {
                endpointKey = ComposeTestEndpointKey(userId);
            }

            var endpoint = await _db.Endpoints.FindAsync(new object[] { endpointKey, userId }, cancellationToken);

            if (endpoint == null)
            {
                return new { ResultType = SubmitClientResultType.OkWithMessage, ResultValue = "Endpoint not found" }
            }
            ;

            _db.Endpoints.Remove(endpoint);
            await _db.SaveChangesAsync(cancellationToken);

            return(new { ResultType = SubmitClientResultType.OkWithMessage, ResultValue = $"Endpoint {endpoint.Endpoint} was removed" });
        }
Ejemplo n.º 2
0
        public async Task AddAsync(User user)
        {
            await _context.User.AddAsync(user.ToEntity());

            await _context.SaveChangesAsync();

            return;
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Create([Bind("Id,Host,Username,Password")] ServerModel Server)
        {
            if (ModelState.IsValid)
            {
                _context.Add(Server);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(Server));
        }
Ejemplo n.º 4
0
        public async Task <AnticipationResult> Create(List <int> paymentIds, DateTime solicitationDate)
        {
            // Check if there's a open solicitation
            var openAnalysis = await _dbContext.Set <AnticipationAnalysis>()
                               .Where(x => x.EndDate == null)
                               .FirstOrDefaultAsync();

            if (openAnalysis != null)
            {
                return(_resultService.GenerateFailedResult("Cannot open solicitation without finilizing the current one.", true));
            }

            var payments = await _dbContext.Set <PaymentEntity>()
                           .Where(x => paymentIds.Contains(x.Id) && x.AnticipationId == null && x.Approved == true)
                           .ToListAsync();

            if (payments.Count() == 0)
            {
                return(_resultService.GenerateFailedResult("None of the solicited payments are available for anticipation.", true));
            }


            var entity = new AnticipationEntity
            {
                SolicitationDate = solicitationDate,
                SolicitedValue   = 0,
            };

            await _dbContext.AddAsync(entity);

            await _dbContext.SaveChangesAsync();

            // sets the payment.solicitationId fields to the current anticipation id
            // needs to be done after the SaveChangesAsync so the id is set.
            foreach (var payment in payments)
            {
                payment.AnticipationId = entity.Id;
                entity.SolicitedValue += payment.LiquidValue * TaxRate;
            }

            var analysis = new AnticipationAnalysis
            {
                AnticipationId = entity.Id
            };

            await _dbContext.AddAsync(analysis);

            await _dbContext.SaveChangesAsync();

            await FillInternalEntities(entity);

            return(_resultService.GenerateResult(entity));
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Create([Bind("Id,ChannelName,ServerModelId")] ChannelModel channel)
        {
            if (ModelState.IsValid)
            {
                _context.Add(channel);
                var server = _context.Servers.Find(channel.ServerModelId);
                server.Channels.Add(channel);
                _context.Update(server);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Edit", "Servers", new { Id = channel.ServerModelId }));
            }
            return(View(channel));
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> Post([FromBody] Settings settings)
        {
            this.context.Settings.Attach(settings).State = EntityState.Modified;
            await context.SaveChangesAsync();

            return(Ok());
        }
Ejemplo n.º 7
0
        public async Task <T> Create(T entity)
        {
            await _dbContext.AddAsync(entity);

            await _dbContext.SaveChangesAsync();

            return(entity);
        }
Ejemplo n.º 8
0
        public async Task <PaymentEntity> Create(PaymentEntity entity)
        {
            await _dbContext.AddAsync(entity);

            await _dbContext.SaveChangesAsync();

            return(entity);
        }
Ejemplo n.º 9
0
        public async Task SetStatus()
        {
            var first = context.HeartBeatInfo.First();

            first.FailsInRow    = 0;
            first.LastTimeAlive = DateTime.Now;

            await context.SaveChangesAsync();

            await hubContext.Clients.All.SendAsync(RoomHub.StatusUpdate, first.GetStatus().ToString());
        }
Ejemplo n.º 10
0
        public async Task <ActionResult> Add()
        {
            var raw = await Request.GetRawBodyStringAsync();

            var snapshot = parser.Parse(raw, DateTime.Now);

            await _context.TemperatureSensorSnapshots.AddAsync(snapshot);

            await _context.SaveChangesAsync();

            await roomHub.Clients.All.SendAsync(RoomHub.RoomUpdate, snapshot);

            return(Ok());
        }
Ejemplo n.º 11
0
            protected override async Task <object[]> OnWorkAsync()
            {
                _targetPoint.Quantity -= _quantity;
                _point.Quantity       += _quantity;
                await _gameDbContext.SaveChangesAsync(); // 동시성 문제가 발생 가능

                return(new[]
                {
                    new
                    {
                        point = _point,
                        targetPoint = _targetPoint,
                    },
                });
            }
Ejemplo n.º 12
0
        /// <summary>
        /// 提交当前操作的结果
        /// </summary>
        /// <returns></returns>
        public async Task <int> CommitTrans()
        {
            try
            {
                int returnValue = await _dbContext.SaveChangesAsync();

                if (dbContextTransaction != null)
                {
                    await dbContextTransaction.CommitAsync();
                }
                return(returnValue);
            }
            catch
            {
                throw;
            }
        }
Ejemplo n.º 13
0
        public async Task <IActionResult> Gain([FromBody] PointGainRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var claim = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier);

            if (claim == null)
            {
                return(BadRequest());
            }
            var id        = int.Parse(claim.Value);                    //
            var pointType = Enum.Parse <PointType>(request.PointType); // ArgumentException

            var point = await _gameDbContext.Point.FirstOrDefaultAsync(i => i.Type == pointType);

            if (point == null)
            {
                point = new Point()
                {
                    UserId     = id,
                    Type       = pointType,
                    Quantity   = 0,
                    CreateTime = DateTime.UtcNow,
                    Deleted    = false,
                };
                await _gameDbContext.Point.AddAsync(point);
            }

            point.Quantity += request.Quantity;
            await _gameDbContext.SaveChangesAsync(); // 아직 트랜잭션이 필요하진 않아

            return(Ok(point));
        }
Ejemplo n.º 14
0
        public async Task <IActionResult> SignUp([FromBody] UserSignUpRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var user = await _accountDbContext.User.FirstOrDefaultAsync(i => i.Name == request.Name);

            if (user != null)
            {
                return(BadRequest());
            }

            user = new Entity.User()
            {
                Name       = request.Name,
                Password   = request.Password,
                CreateTime = DateTime.UtcNow,
                Deleted    = false,
            };
            await _accountDbContext.User.AddAsync(user);

            await _accountDbContext.SaveChangesAsync();

            // 기본 설정
            var gold = new Entity.Point()
            {
                UserId     = user.Id,
                Type       = Entity.PointType.Gold,
                Quantity   = 100,
                CreateTime = DateTime.UtcNow,
                Deleted    = false,
            };
            await _gameDbContext.Point.AddAsync(gold);

            var silver = new Entity.Point()
            {
                UserId     = user.Id,
                Type       = Entity.PointType.Silver,
                Quantity   = 100,
                CreateTime = DateTime.UtcNow,
                Deleted    = false,
            };
            await _gameDbContext.Point.AddAsync(silver);

            await _gameDbContext.SaveChangesAsync();

            return(Ok(new
            {
                user = new
                {
                    id = user.Id,
                    name = user.Name,
                    createTime = user.CreateTime,
                    role = "Admin",
                },
                points = new[]
                {
                    new
                    {
                        type = gold.Type.ToString(),
                        quantity = gold.Quantity,
                    },
                    new
                    {
                        type = silver.Type.ToString(),
                        quantity = silver.Quantity,
                    },
                },
            }));
        }
Ejemplo n.º 15
0
 public Task SaveChangesAsync() => _appContext.SaveChangesAsync();