Exemplo n.º 1
0
        public async Task <IActionResult> InsertUpdateConsultant(Guid?Id)
        {
            //throw new Exception("Error in Details View");

            logger.LogTrace("Trace Log");
            logger.LogDebug("Debug Log");
            logger.LogInformation("Information Log");
            logger.LogWarning("Warning Log");
            logger.LogError("Error Log");
            logger.LogCritical("Critical Log");

            ConsultantVM consultantVM = new ConsultantVM();

            if (Id.HasValue && Id != Guid.Empty)
            {
                consultantVM = await _consultantRepository.GetConsultantDetail(new Guid(Id.ToString()));

                if (consultantVM == null)
                {
                    return(View("Index"));
                }
            }

            return(View(consultantVM));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> InsertUpdateConsultant(ConsultantVM consultantModel)
        {
            if (ModelState.IsValid)
            {
                if (consultantModel.Id == Guid.Empty)
                {
                    if (await _consultantRepository.SaveConsultant(consultantModel) == Shared.Helpers.DbStatusCode.Created)
                    {
                        TempData["ConsultantActionResponse"] = true;
                    }
                    else
                    {
                        TempData["ConsultantActionResponse"] = false;
                    }
                }
                else
                {
                    if (await _consultantRepository.UpdateConsultant(consultantModel) == Shared.Helpers.DbStatusCode.Updated)
                    {
                        TempData["ConsultantActionResponse"] = true;
                    }
                    else
                    {
                        TempData["ConsultantActionResponse"] = false;
                    }
                }
            }
            else
            {
                return(View(consultantModel));
            }

            return(RedirectToAction("Index"));
        }
        public async Task <DbStatusCode> SaveConsultant(ConsultantVM model)
        {
            try
            {
                genericRepository.Add(new ConsultantModel()
                {
                    Id        = Guid.NewGuid(),
                    FirstName = model.FirstName,
                    LastName  = model.LastName,
                    Email     = model.Email,
                    IsActive  = model.IsActive,
                    IsAdmin   = model.IsAdmin,
                    CompanyId = model.CompanyId,
                    UserId    = model.UserId
                });
                var changedVal = await unitOfWork.Commit();

                if (changedVal > 0)
                {
                    return(DbStatusCode.Created);
                }
                else
                {
                    return(DbStatusCode.DbError);
                }
            }
            catch (Exception ex)
            {
                //logging an exception
                return(DbStatusCode.Exception);
            }
        }
        public async Task <IActionResult> InsertNewConsultant(ConsultantVM consultantVM)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    consultantVM.UserId = User.FindFirstValue(ClaimTypes.Name);
                    var result = await consultantService.SaveConsultant(consultantVM);

                    if (result == DbStatusCode.Created)
                    {
                        return(Ok(HttpStatusCode.Created));
                    }
                    else if (result == DbStatusCode.Exception)
                    {
                        return(StatusCode(500));
                    }
                    else
                    {
                        return(Forbid());
                    }
                    //We can also return status like below
                    //return StatusCode(403);       //Forbidden: 403
                }
                else
                {
                    return(BadRequest(ModelState));
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message ?? ex.InnerException.Message));
            }
        }
        public async Task <DbStatusCode> UpdateConsultant(ConsultantVM model)
        {
            try
            {
                //var consultantModel = await GetConsultantDetail(model.Id);
                var consultantModel = await genericRepository.Get(e => e.Id == model.Id).FirstOrDefaultAsync();

                if (consultantModel != null)
                {
                    consultantModel.FirstName = model.FirstName;
                    consultantModel.LastName  = model.LastName;
                    consultantModel.Email     = model.Email;
                    consultantModel.IsActive  = model.IsActive;
                    consultantModel.IsAdmin   = model.IsAdmin;
                    consultantModel.UserId    = model.UserId;

                    genericRepository.Update(consultantModel);
                    var changedVal = await unitOfWork.Commit();

                    if (changedVal > 0)
                    {
                        return(DbStatusCode.Updated);
                    }
                    else
                    {
                        //Set the response status
                        return(DbStatusCode.DbError);
                    }
                }
                else
                {
                    return(DbStatusCode.NotFound);
                }
            }
            catch (Exception ex)
            {
                //logging an exception
                return(DbStatusCode.Exception);
            }
        }
        public async Task <ConsultantVM> GetConsultantDetail(Guid Id)
        {
            ConsultantVM consultantVM = new ConsultantVM();

            var consultantModel = await genericRepository.Get(e => e.Id == Id).FirstOrDefaultAsync();

            if (consultantModel != null)
            {
                consultantVM.Id        = consultantModel.Id;
                consultantVM.FirstName = consultantModel.FirstName;
                consultantVM.LastName  = consultantModel.LastName;
                consultantVM.Email     = consultantModel.Email;
                consultantVM.IsActive  = consultantModel.IsActive;
                consultantVM.IsAdmin   = consultantModel.IsAdmin;
                consultantVM.CompanyId = consultantModel.CompanyId;
            }
            else
            {
                return(null);
            }

            return(consultantVM);
        }
        public async Task <IActionResult> UpdateConsultant(ConsultantVM consultantVM)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    consultantVM.UserId = User.FindFirstValue(ClaimTypes.Name);
                    var result = await consultantService.UpdateConsultant(consultantVM);

                    if (result == DbStatusCode.Updated)
                    {
                        return(Ok());
                    }
                    else if (result == DbStatusCode.NotFound)
                    {
                        return(StatusCode(404));
                    }
                    else if (result == DbStatusCode.Exception)
                    {
                        return(StatusCode(500));
                    }
                    else
                    {
                        return(Forbid());
                    }
                }
                else
                {
                    return(BadRequest(ModelState));
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message ?? ex.InnerException.Message));
            }
        }