Ejemplo n.º 1
0
        public async Task <ActionResult <Applicant> > Create([FromBody] object jsonObj)
        {
            try
            {
                Applicant           applicant        = Newtonsoft.Json.JsonConvert.DeserializeObject <Applicant>(jsonObj.ToString());
                ApplicantValidation validationRules  = new ApplicantValidation();
                ValidationResult    validationResult = validationRules.Validate(applicant);
                if (!validationResult.IsValid)
                {
                    Dictionary <string, string> errResult = new Dictionary <string, string>();
                    foreach (ValidationFailure failure in validationResult.Errors)
                    {
                        errResult.Add(failure.PropertyName, failure.ErrorMessage);
                    }

                    var result = new JsonResult(errResult);
                    result.StatusCode = 400;
                    return(result);
                }
                else
                {
                    context.Applicants.Add(applicant);
                    await context.SaveChangesAsync();

                    return(CreatedAtAction(nameof(Get), new { id = applicant.ID }, applicant));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex
                                 , "Could not create new applicant. {msg}", ex.Message);
                return(BadRequest());
            }
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <Applicant> > Update(int applicantId, [FromBody] Applicant newData)
        {
            try
            {
                var     applicant = context.Applicants.FirstOrDefault(x => x.ID == applicantId);
                JObject errObj    = new JObject();

                if (applicant == null)
                {
                    errObj.Add("Error", "Applicant does not exists");
                    var result = new JsonResult(errObj);
                    result.StatusCode = 400;
                    return(result);
                }

                ApplicantValidation validationRules  = new ApplicantValidation();
                ValidationResult    validationResult = validationRules.Validate(newData);
                if (!validationResult.IsValid)
                {
                    foreach (ValidationFailure failure in validationResult.Errors)
                    {
                        errObj.Add(failure.PropertyName, failure.ErrorMessage);
                    }

                    var result = new JsonResult(errObj);
                    result.StatusCode = 400;
                    return(result);
                }

                applicant.Name            = newData.Name;
                applicant.FamilyName      = newData.FamilyName;
                applicant.Address         = newData.Address;
                applicant.CountryOfOrigin = newData.CountryOfOrigin;
                applicant.EmailAddress    = newData.EmailAddress;
                applicant.Age             = newData.Age;
                applicant.Hired           = newData.Hired;

                context.Entry(applicant).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                await context.SaveChangesAsync();

                return(NoContent());
            }
            catch (Exception ex)
            {
                _logger.LogError(ex
                                 , "Error when updating applicant with id: {id}. {msg}", applicantId, ex.Message);
                return(BadRequest());
            }
        }