Esempio n. 1
0
        public async Task <(bool Success, string Error)> SaveJobPostAsync(CreateJobPostViewModel model, string userId)
        {
            var success = false;
            var error   = string.Empty;

            try
            {
                JobPost jobPost = null;
                if (model.JobPostId > 0)
                {
                    jobPost = await _jobRepository.FetchByIdAsync(model.JobPostId.Value);
                }
                if (jobPost == null)
                {
                    jobPost = new JobPost
                    {
                        CreatedUserId = userId,
                        CreatedUtc    = DateTime.UtcNow
                    };
                }
                else
                {
                    jobPost.AuditUserId = userId;
                    jobPost.AuditUtc    = DateTime.UtcNow;
                }

                // save other values
                jobPost.JobTitle     = model.JobTitle;
                jobPost.Description  = model.Description;
                jobPost.Requirements = model.Requirements;
                jobPost.JobTypeId    = model.JobTypeId;
                jobPost.JobStartDate = model.JobStartDate;
                jobPost.Length       = model.Length;
                jobPost.Pay          = model.Pay;
                jobPost.JobLocation  = model.JobLocation;
                if (model.JobCloseDate.HasValue)
                {
                    jobPost.JobCloseUtc = _dateTimeService.ConvertDateTimeToUtc(model.JobCloseDate.Value);
                }
                else
                {
                    jobPost.JobCloseUtc = null;
                }
                jobPost.IsDraft  = model.IsDraft;
                jobPost.IsClosed = model.IsClosed;

                await _jobRepository.SaveAsync(jobPost);

                success = true;
            }
            catch (Exception ex)
            {
                error = "An unexpected error has occurred.";
                _logger.LogError("JobPostService.SaveJobPostAsync - exception:{@Ex}", ex);
            }

            return(success, error);
        }
Esempio n. 2
0
        public async Task <(bool Success, string Error, CreateJobPostViewModel Model)> FetchJobPostForEditAsync(int?id)
        {
            var success = false;
            var error   = string.Empty;
            var model   = new CreateJobPostViewModel();

            try
            {
                if (id > 0)
                {
                    var jobPost = await _jobRepository.FetchByIdAsync(id.Value);

                    if (jobPost != null)
                    {
                        model.JobPostId    = jobPost.JobPostId;
                        model.JobTitle     = jobPost.JobTitle;
                        model.Description  = jobPost.Description;
                        model.Requirements = jobPost.Requirements;
                        model.JobTypeId    = jobPost.JobTypeId;
                        model.JobStartDate = jobPost.JobStartDate;
                        model.Length       = jobPost.Length;
                        model.Pay          = jobPost.Pay;
                        model.JobLocation  = jobPost.JobLocation;
                        if (jobPost.JobCloseUtc.HasValue)
                        {
                            model.JobCloseDate = _dateTimeService.ConvertUtcToDateTime(jobPost.JobCloseUtc.Value,
                                                                                       _dateTimeService.FetchTimeZoneInfo(Constants.System.TimeZone));
                        }
                        model.IsDraft  = jobPost.IsDraft;
                        model.IsClosed = jobPost.IsClosed;

                        success = true;
                    }
                    else
                    {
                        error = "Unable to locate job post";
                    }
                }
                else
                {
                    error = "Invalid Request";
                }
            }
            catch (Exception ex)
            {
                error = "An unexpected error has occurred.";
                _logger.LogError("JobPostService.FetchJobPostForEditAsync - exception:{@Ex}", ex);
            }

            return(success, error, model);
        }
Esempio n. 3
0
        public async Task <IActionResult> Create(CreateJobPostViewModel model)
        {
            _logger.LogInformation(GetLogDetails() + " - model:{@Model}", model);

            if (ModelState.IsValid)
            {
                var userId = _userManager.GetUserId(User);
                var result = await _jobPostService.SaveJobPostAsync(model, userId);

                if (result.Success)
                {
                    SetSiteMessage(MessageType.Success, DisplayFor.FullRequest, "Job post has been saved successfully");
                    return(RedirectToAction("List"));
                }
                else
                {
                    SetSiteMessage(MessageType.Error, DisplayFor.FullRequest, result.Error);
                }
            }

            SetSiteMessage(MessageType.Error, DisplayFor.FullRequest, "Please review the form and try again.");
            return(View(model));
        }