Exemplo n.º 1
0
        public async Task <KotaResponse> DeletePost(KotaRequest request)
        {
            KotaResponse response = new KotaResponse();

            if (db != null)
            {
                try
                {
                    Kota model = await db.Kota.Where(x => x.RowStatus == true && x.Id == request.ID).FirstOrDefaultAsync();

                    if (model != null)
                    {
                        model.Modified   = DateTime.Now;
                        model.ModifiedBy = request.UserName;
                        model.RowStatus  = false;
                        await db.SaveChangesAsync();

                        response.IsSuccess = true;
                        response.Message   = "Data has been Saved";
                    }
                    else
                    {
                        response.IsSuccess = false;
                        response.Message   = "Data Not Found";
                    }
                }
                catch (Exception ex)
                {
                    response.Message   = ex.ToString();
                    response.IsSuccess = false;
                }
            }
            return(response);
        }
Exemplo n.º 2
0
        public async Task <KotaResponse> AddPost(KotaRequest req)
        {
            KotaResponse response = new KotaResponse();

            try
            {
                Kota model = new Kota();
                model.ProvinsiId  = req.ProvinsiID;
                model.Name        = req.Name;
                model.Description = req.Description;
                model.Created     = DateTime.Now;
                model.CreatedBy   = req.UserName;
                model.RowStatus   = true;
                await dep.AddPost(model);

                response.Message   = "Success";
                response.IsSuccess = true;
            }
            catch (Exception)
            {
                response.Message   = "Failed";
                response.IsSuccess = false;
            }
            return(response);
        }
Exemplo n.º 3
0
        public async Task <KotaResponse> GetPost(KotaRequest req)
        {
            KotaResponse response = new KotaResponse();
            var          model    = await dep.GetPost(req);

            if (model == null)
            {
                return(null);
            }
            return(response);
        }
Exemplo n.º 4
0
        public async Task <KotaResponse> DeletePost(KotaRequest req)
        {
            KotaResponse resp = new KotaResponse();

            try
            {
                string bearer   = Request.HttpContext.Request.Headers["Authorization"];
                string token    = bearer.Substring("Bearer ".Length).Trim();
                string username = string.Empty;
                if (string.IsNullOrEmpty(token))
                {
                    resp.IsSuccess = false;
                    resp.Message   = "You don't have access.";
                    return(resp);
                }

                username = sec.ValidateToken(token);
                if (username == null)
                {
                    Response.HttpContext.Response.Cookies.Append("access_token", "", new CookieOptions()
                    {
                        Expires = DateTime.Now.AddDays(-1)
                    });
                    resp.IsSuccess = false;
                    resp.Message   = "Your session was expired, please re-login.";
                    return(resp);
                }
                req.UserName = username;



                return(resp = await facade.DeletePost(req));
            }
            catch (Exception ex)
            {
                resp.IsSuccess = false;
                resp.Message   = ex.Message.ToString();
                return(resp);
            }

            //try
            //{
            //    var result = await facade.DeletePost(req);

            //    return Ok(result);
            //}
            //catch (Exception)
            //{

            //    return BadRequest();
            //}
        }
Exemplo n.º 5
0
        public async Task <KotaResponse> GetAllWithoutFilter()
        {
            KotaResponse response = new KotaResponse();

            var listKota = await dep.GetAllWithoutFilter();

            response.ListKota = (from kota in listKota
                                 select new KotaViewModel
            {
                ID = kota.Id,
                Name = kota.Name
            }).ToList();
            return(response);
        }
Exemplo n.º 6
0
        public async Task <IActionResult> GetAllForDropdown(int ProvinsiID)
        {
            KotaResponse response = new KotaResponse();

            try
            {
                response.ListKotas = await facade.GetAllForDropdown(ProvinsiID);

                response.IsSuccess = true;
                response.Message   = "Success";
            }
            catch (Exception ex)
            {
                response.IsSuccess = false;
                response.Message   = ex.ToString();
                return(BadRequest(ex));
            }
            return(Ok(response));
        }
Exemplo n.º 7
0
        public async Task <KotaResponse> UpdatePost(KotaRequest req)
        {
            KotaResponse response = new KotaResponse();

            try
            {
                Kota model = new Kota();
                model = await dep.GetPost(req);


                model.ProvinsiId = req.ProvinsiID;
                model.Name       = req.Name;

                model.Description = req.Description;
                model.Modified    = DateTime.Now;
                model.ModifiedBy  = req.UserName;
                model.RowStatus   = true;
                await dep.UpdatePost(model);

                var result = await dep.UpdatePost(model);

                if (result)
                {
                    response.Message   = "Success";
                    response.IsSuccess = true;
                }
                else
                {
                    response.Message   = "Failed";
                    response.IsSuccess = false;
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
                response.Message   = "Failed";
                response.IsSuccess = false;
            }
            return(response);
        }
Exemplo n.º 8
0
        public async Task <KotaResponse> GetAll()
        {
            KotaResponse resp = new KotaResponse();

            try
            {
                string bearer   = Request.HttpContext.Request.Headers["Authorization"];
                string token    = bearer.Substring("Bearer ".Length).Trim();
                string username = string.Empty;
                if (string.IsNullOrEmpty(token))
                {
                    resp.IsSuccess = false;
                    resp.Message   = "You don't have access.";
                    return(resp);
                }

                username = sec.ValidateToken(token);
                if (username == null)
                {
                    Response.HttpContext.Response.Cookies.Append("access_token", "", new CookieOptions()
                    {
                        Expires = DateTime.Now.AddDays(-1)
                    });
                    resp.IsSuccess = false;
                    resp.Message   = "Your session was expired, please re-login.";
                    return(resp);
                }



                string search   = HttpContext.Request.Query["search[value]"].ToString();
                int    draw     = Convert.ToInt32(HttpContext.Request.Query["draw"]);
                string order    = HttpContext.Request.Query["order[0][column]"];
                string orderDir = HttpContext.Request.Query["order[0][dir]"];
                int    startRec = Convert.ToInt32(HttpContext.Request.Query["start"]);
                int    pageSize = Convert.ToInt32(HttpContext.Request.Query["length"]);
                resp = await facade.GetAll(search, order, orderDir, startRec, pageSize, draw);

                return(resp);
            }
            catch (Exception ex)
            {
                resp.IsSuccess = false;
                resp.Message   = ex.Message.ToString();
                return(resp);
            }
            //try
            //{
            //    string search = HttpContext.Request.Query["search[value]"].ToString();
            //    int draw = Convert.ToInt32(HttpContext.Request.Query["draw"]);
            //    string order = HttpContext.Request.Query["order[0][column]"];
            //    string orderDir = HttpContext.Request.Query["order[0][dir]"];
            //    int startRec = Convert.ToInt32(HttpContext.Request.Query["start"]);
            //    int pageSize = Convert.ToInt32(HttpContext.Request.Query["length"]);
            //    var models = await facade.GetAll(search, order, orderDir, startRec, pageSize, draw);

            //    if (models == null)
            //    {
            //        return NotFound();
            //    }

            //    return Ok(models);
            //}
            //catch (Exception ex)
            //{
            //    return BadRequest(ex);
            //}
        }
Exemplo n.º 9
0
        public async Task <KotaResponse> GetAll(string search, string order, string orderDir, int startRec, int pageSize, int draw)
        {
            KotaResponse response = new KotaResponse();

            if (db != null)
            {
                try
                {
                    var query = (from kota in db.Kota
                                 join provinsi in db.Provinsi
                                 on kota.ProvinsiId equals provinsi.Id
                                 where kota.RowStatus == true && provinsi.RowStatus == true
                                 select new
                    {
                        ID = kota.Id,
                        Name = kota.Name,
                        kota.Description,
                        ProvinsiID = kota.ProvinsiId,
                        ProvinsiName = provinsi.Name
                    }
                                 );
                    int totalRecords = query.Count();
                    if (!string.IsNullOrEmpty(search) && !string.IsNullOrWhiteSpace(search))
                    {
                        query = query.Where(p => p.Name.ToString().ToLower().Contains(search.ToLower()) ||
                                            p.ProvinsiName.ToLower().Contains(search.ToLower()) ||
                                            p.Description.ToLower().Contains(search.ToLower()));
                    }
                    int recFilter = query.Count();

                    response.ListKota = await(from model in query
                                              select new KotaViewModel
                    {
                        ID           = model.ID,
                        Name         = model.Name,
                        Description  = model.Description,
                        ProvinsiID   = model.ProvinsiID,
                        ProvinsiName = model.ProvinsiName,
                    }).Skip(startRec).Take(pageSize).ToListAsync();

                    //response.ListProvinsi = await (from prov in db.Provinsi
                    //                               where prov.RowStatus == true
                    //                               select new ProvinsiViewModel
                    //                               {
                    //                                   ID = prov.Id,
                    //                                   Nama = prov.Nama,
                    //                                   Created = prov.Created,
                    //                                   CreatedBy = prov.CreatedBy,
                    //                                   Description = prov.Description,
                    //                                   Modified = prov.Modified,
                    //                                   ModifiedBy = prov.ModifiedBy,
                    //                                   RowStatus = prov.RowStatus
                    //                               }).ToListAsync();
                    response.Message         = "Success";
                    response.draw            = Convert.ToInt32(draw);
                    response.recordsTotal    = totalRecords;
                    response.recordsFiltered = recFilter;
                    response.IsSuccess       = true;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            return(response);
        }