Exemplo n.º 1
0
        public virtual async Task <ActionResult <T> > Post([FromBody] T entity)
        {
            // Validate dữ liệu theo các Attribure Property
            if (!ModelState.IsValid)
            {
                var resultValidate = new ActionServiceResult()
                {
                    Success  = false,
                    Messenge = Resources.ErrorValidate_NotValid,
                    MISACode = MISACode.ValidateEntity,
                    Data     = ModelState
                };
                return(BadRequest(resultValidate));
            }

            var result = await _baseEntityService.Insert(entity, true);

            if (result.Success == false)
            {
                return(BadRequest(result));
            }
            var tableName = entity.GetType().Name;

            return(Created("Created " + tableName, result.Data));
        }
Exemplo n.º 2
0
        public virtual async Task <ActionServiceResult> Post([FromBody] T entity)
        {
            var response = new ActionServiceResult();

            // Validate dữ liệu theo các Attribure Property
            if (!ModelState.IsValid)
            {
                response.Success = false;
                response.Message = Resources.ValidateEntity;
                response.Code    = Code.ValidateEntity;
                response.Data    = ModelState;
            }
            else
            {
                var result = await _baseEntityService.Insert(entity, true);

                if (result.Success == false)
                {
                    response.Success = false;
                    response.Code    = Code.ErrorAddEntity;
                    response.Message = Resources.ErrorAddEntity;
                }
            }
            return(response);
        }
        public async Task <IActionResult> Register(UserG12 userG12)
        {
            if (userG12 == null)
            {
                return(BadRequest(
                           new ActionServiceResult()
                {
                    Success = false,
                    Code = Code.ValidateEntity,
                    Message = Resources.ValidateEntity,
                    Data = null
                }));
            }
            var url = _uri + "api/Intergrates/register";

            var httpClient = new HttpClient();

            var jsonSettings = new JsonSerializerSettings()
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                NullValueHandling     = NullValueHandling.Ignore
            };
            var dict = new StringContent(JsonConvert.SerializeObject(userG12, Formatting.None, jsonSettings), Encoding.UTF8, "application/json");


            var res = (await httpClient.PostAsync(url, dict));

            if (res.StatusCode == System.Net.HttpStatusCode.OK)
            {
                var result = JsonConvert.DeserializeObject <ResObjectG12 <User> >(res.Content.ReadAsStringAsync().Result);
                if (result.Code == 200 && result.Data != null)
                {
                    //update db
                    result.Data.RoleId = Guid.Parse("01625518-9205-2988-5145-017982868048");
                    await _baseUserService.Insert(result.Data, true);

                    return(Ok(result.Data.UserID));
                }
            }

            return(BadRequest());
        }
Exemplo n.º 4
0
        public async Task <ActionServiceResult> GetEntityByID([FromQuery] string contestID)
        {
            StringValues userHeader;

            Request.Headers.TryGetValue("UserID", out userHeader);
            var userID   = userHeader.FirstOrDefault().ToString();
            var token    = Request.Headers["Authorization"].ToString();
            var roleName = Utils.GetClaimFromToken(token, "rolename") == "" ? "student" : Utils.GetClaimFromToken(token, "rolename");

            var result = new ActionServiceResult();

            if (userID == null || string.IsNullOrEmpty(userID) || string.IsNullOrEmpty(contestID))
            {
                result.Success = false;
                result.Code    = Code.NotFound;
            }
            else
            {
                var response = await _examService.GetByUserID(contestID);

                switch (roleName)
                {
                case "lecture":
                    result.Data = response;
                    return(result);

                case "student":
                    var exam = response.Where(item => item.UserId.ToString() == userID).FirstOrDefault();
                    result.Data = exam;

                    if (exam == null)
                    {
                        //Handle goi api tao de thi tu nhom 10
                        var res = JsonConvert.DeserializeObject("[{'Question':'Day la cau hoi','type':1,'Answer':'|Dap an 1|Dap an 2 |Dap an 3'},{ 'Question':'Day la cau hoi','type':1,'Answer':'|Dap an 1|Dap an 2 |Dap an 3'},{ 'Question':'Day la cau hoi','type':1,'Answer':'|Dap an 1|Dap an 2 |Dap an 3'}]");
                        //
                        var examRes = new Exam();
                        examRes.ContestId    = Guid.Parse(contestID);
                        examRes.CreatedDate  = DateTime.Now;
                        examRes.ModifiedDate = DateTime.Now;
                        examRes.ExamId       = Guid.NewGuid();
                        examRes.UserId       = Guid.Parse(userID);
                        examRes.Question     = response;
                        //exam.Answer = response.Answer;
                        examRes.IsDoing = 1;
                        examRes.Status  = 0;

                        await _baseEntityService.Insert(examRes);

                        return(new ActionServiceResult(true, Resources.Success, Code.Success, examRes, 0));
                    }
                    else
                    {
                        exam.ModifiedDate = DateTime.Now;
                        result.Data       = exam;
                        await _baseEntityService.Update(exam);

                        return(result);
                    }

                default:
                    break;
                }
            }
            return(result);
        }