示例#1
0
        public async Task <IActionResult> Register([FromBody] ManagerViewModel body)
        {
            try
            {
                var entity = await _unitOfWork.Managers.GetAsync(u => u.Id == body.Id);

                if (entity != null)
                {
                    return(Payloader.Fail(PayloadCode.Duplication));
                }

                var newEntity = new Manager
                {
                    Id       = body.Id,
                    Password = body.Password,
                };

                await _unitOfWork.Managers.AddAsync(newEntity);

                await _unitOfWork.CommitAsync();

                return(Payloader.Success(_mapper.Map <ManagerViewModel>(newEntity)));
            }
            catch (Exception ex)
            {
                return(Payloader.Error(ex));
            }
        }
        public async Task <IActionResult> Create([FromBody] VideoViewModel body)
        {
            try
            {
                var entity = await _unitOfWork.Videos.GetAsync(u => u.Id == body.Id);

                if (entity != null)
                {
                    return(Payloader.Fail(PayloadCode.Duplication));
                }

                var newEntity = new Video
                {
                    Title = body.Title,
                    Uri   = body.Uri,
                };

                await _unitOfWork.Videos.AddAsync(newEntity);

                await _unitOfWork.CommitAsync();

                return(Payloader.Success(_mapper.Map <VideoViewModel>(newEntity)));
            }
            catch (Exception ex)
            {
                return(Payloader.Error(ex));
            }
        }
示例#3
0
        public async Task <IActionResult> Get()
        {
            try
            {
                var authToken   = Request.Headers["Authorization"].ToString();
                var tokenString = authToken.Substring(7);

                var tokenHandler = new JwtSecurityTokenHandler();
                var token        = tokenHandler.ReadToken(tokenString);
                var userId       = ((JwtSecurityToken)token).Payload["nameid"].ToString();

                var entity = await _unitOfWork.Managers.GetAsync(u => u.Id == userId);

                if (entity == null)
                {
                    return(Payloader.Fail(PayloadCode.DbNull));
                }

                return(Payloader.Success(_mapper.Map <ManagerViewModel>(entity)));
            }
            catch (Exception ex)
            {
                return(Payloader.Error(ex));
            }
        }
示例#4
0
        public async Task <IActionResult> Create([FromBody] CreateUserBody body)
        {
            try
            {
                var entity = await _unitOfWork.Users.GetAsync(x => x.Id == body.Id);

                if (entity != null)
                {
                    return(Payloader.Fail(PayloadCode.Duplication));
                }

                var newEntity = new User
                {
                    Id    = body.Id,
                    Name  = body.Name,
                    Money = 1000,
                };

                await UpdateCube(newEntity);

                await _unitOfWork.Users.AddAsync(newEntity);

                await _unitOfWork.CommitAsync();

                return(Payloader.Success(_mapper.Map <UserViewModel>(newEntity)));
            }
            catch (Exception ex)
            {
                return(Payloader.Error(ex));
            }
        }
示例#5
0
        public async Task <IActionResult> Parsing()
        {
            try
            {
                await ParsingCubeData();

                return(Payloader.Success("Parse Success"));
            }
            catch (Exception ex)
            {
                return(Payloader.Error(ex));
            }
        }
        public async Task <IActionResult> Get()
        {
            try
            {
                var entries = await _unitOfWork.Managers.GetManyAsync();

                if (entries == null)
                {
                    return(Payloader.Fail(PayloadCode.DbNull));
                }

                return(Payloader.Success(_mapper.Map <IEnumerable <ManagerViewModel> >(entries)));
            }
            catch (Exception ex)
            {
                return(Payloader.Error(ex));
            }
        }
        public async Task <IActionResult> Get(string id)
        {
            try
            {
                var entity = await _unitOfWork.Managers.GetAsync(u => u.Id == id);

                if (entity == null)
                {
                    return(Payloader.Fail(PayloadCode.DbNull));
                }

                return(Payloader.Success(_mapper.Map <ManagerViewModel>(entity)));
            }
            catch (Exception ex)
            {
                return(Payloader.Error(ex));
            }
        }
示例#8
0
        public async Task <IActionResult> ParsingSkillData(IFormFile xml = null)
        {
            try
            {
                var datas = XmlKey.CubeData.FindAll <SkillDataXml.Data>(xml, new SkillDataXml());

                await _unitOfWork.SkillDatas.DeleteManyAsync();

                await _unitOfWork.SkillDatas.AddAsync(_mapper.Map <IEnumerable <SkillData> >(datas));

                await _unitOfWork.CommitAsync();

                return(Payloader.Success(datas.Count));
            }
            catch (Exception ex)
            {
                return(Payloader.Error(ex));
            }
        }
示例#9
0
        public async Task <IActionResult> UpdateLv(string userId, int cubeId)
        {
            try
            {
                var user = await _unitOfWork.Users.GetAsync(x => x.Id == userId, isTracking : true);

                if (user == null)
                {
                    return(Payloader.Fail(PayloadCode.DbNull));
                }

                var lv   = 0;
                var cube = await _unitOfWork.Cubes.GetAsync(x => x.UserId == userId && x.CubeId == cubeId);

                if (cube != null)
                {
                    lv = cube.Lv;
                }

                var price = ServerDefine.CubeLv2Price((byte)lv);

                if (user.Money < price)
                {
                    return(Payloader.Fail(PayloadCode.Not));
                }

                user.Money -= price;

                var newCube = await _unitOfWork.Cubes.UpdateLv(userId, cubeId, 1);

                await _unitOfWork.CommitAsync();

                return(Payloader.Success(new UpdateCubeLvBody()
                {
                    Cube = _mapper.Map <CubeViewModel>(newCube),
                    Money = user.Money,
                }));
            }
            catch (Exception ex)
            {
                return(Payloader.Error(ex));
            }
        }
示例#10
0
        public async Task <IActionResult> Get(string id)
        {
            try
            {
                var entity = await _unitOfWork.Users.GetAsync(x => x.Id == id,
                                                              eagerLoad : q => q.Include(x => x.Entry),
                                                              explicitLoad : q => q.Include(x => x.Cubes).ThenInclude(x => x.CubeData).Load(),
                                                              isTracking : true);

                if (entity == null)
                {
                    return(Payloader.Fail(PayloadCode.DbNull));
                }

                return(Payloader.Success(_mapper.Map <UserViewModel>(entity)));
            }
            catch (Exception ex)
            {
                return(Payloader.Error(ex));
            }
        }
示例#11
0
        public async Task <IActionResult> Update(string userId, [FromBody] int[] slots)
        {
            try
            {
                var entity = await _unitOfWork.Entries.GetAsync(x => x.UserId == userId, isTracking : true);

                if (entity == null)
                {
                    return(Payloader.Fail(PayloadCode.DbNull));
                }

                entity.Slots = slots;

                await _unitOfWork.CommitAsync();

                return(Payloader.Success(slots));
            }
            catch (Exception ex)
            {
                return(Payloader.Error(ex));
            }
        }
        public async Task <IActionResult> Put(string id, [FromBody] ManagerViewModel body)
        {
            try
            {
                var entity = await _unitOfWork.Managers.GetAsync(u => u.Id == id, isTracking : true);

                if (entity == null)
                {
                    return(Payloader.Fail(PayloadCode.DbNull));
                }

                entity.Password = body.Password;

                await _unitOfWork.CommitAsync();

                return(Payloader.Success(_mapper.Map <ManagerViewModel>(entity)));
            }
            catch (Exception ex)
            {
                return(Payloader.Error(ex));
            }
        }
示例#13
0
        public async Task <IActionResult> UpdateMoney(string id, [FromBody] int money)
        {
            try
            {
                var entity = await _unitOfWork.Users.GetAsync(x => x.Id == id, isTracking : true);

                if (entity == null)
                {
                    return(Payloader.Fail(PayloadCode.DbNull));
                }

                entity.Money += money;

                await _unitOfWork.CommitAsync();

                return(Payloader.Success(entity.Money));
            }
            catch (Exception ex)
            {
                return(Payloader.Error(ex));
            }
        }
        public async Task <IActionResult> Delete(string id)
        {
            try
            {
                var entity = await _unitOfWork.Managers.GetAsync(u => u.Id == id);

                if (entity == null)
                {
                    return(Payloader.Fail(PayloadCode.DbNull));
                }

                await _unitOfWork.Managers.DeleteAsync(entity);

                await _unitOfWork.CommitAsync();

                return(Payloader.Success(id));
            }
            catch (Exception ex)
            {
                return(Payloader.Error(ex));
            }
        }
        public async Task <IActionResult> Put(int id, [FromBody] VideoViewModel body)
        {
            try
            {
                var entity = await _unitOfWork.Videos.GetAsync(u => u.Id == id, isTracking : true);

                if (entity == null)
                {
                    return(Payloader.Fail(PayloadCode.DbNull));
                }

                entity.Title = body.Title;
                entity.Uri   = body.Uri;

                await _unitOfWork.CommitAsync();

                return(Payloader.Success(_mapper.Map <VideoViewModel>(entity)));
            }
            catch (Exception ex)
            {
                return(Payloader.Error(ex));
            }
        }
示例#16
0
        public async Task <IActionResult> Put(string id, [FromBody] UserViewModel body)
        {
            try
            {
                var entity = await _unitOfWork.Users.GetAsync(x => x.Id == id, isTracking : true);

                if (entity == null)
                {
                    return(Payloader.Fail(PayloadCode.DbNull));
                }

                //entity.Name = body.Name;
                entity.Money = body.Money;

                await _unitOfWork.CommitAsync();

                return(Payloader.Success(_mapper.Map <UserViewModel>(entity)));
            }
            catch (Exception ex)
            {
                return(Payloader.Error(ex));
            }
        }
示例#17
0
        public async Task <IActionResult> Login([FromBody] ManagerViewModel body)
        {
            try
            {
                var entity = await _unitOfWork.Managers.GetAsync(u => u.Id == body.Id && u.Password == body.Password);

                if (entity == null)
                {
                    return(Payloader.Fail(PayloadCode.DbNull));
                }

                var tokenHandler    = new JwtSecurityTokenHandler();
                var key             = Encoding.ASCII.GetBytes(_configuration.GetSection("AppSettings:Token").Value);
                var tokenDescriptor = new SecurityTokenDescriptor
                {
                    Subject = new ClaimsIdentity(new Claim[] {
                        new Claim(ClaimTypes.NameIdentifier, entity.Id.ToString())
                    }),
                    Expires            = DateTime.Now.AddMinutes(1),
                    SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha512Signature)
                };

                var token       = tokenHandler.CreateToken(tokenDescriptor);
                var tokenString = tokenHandler.WriteToken(token);

                return(Payloader.Success(new
                {
                    manager = _mapper.Map <ManagerViewModel>(entity),
                    token = tokenString,
                }));
            }
            catch (Exception ex)
            {
                return(Payloader.Error(ex));
            }
        }