Skip to content

Commit

Permalink
Merge pull request #194 from ljcordero/feature/191-job-view-counter-e…
Browse files Browse the repository at this point in the history
…nhancement

Feature/191 Job View Counter Enhancement
  • Loading branch information
eatskolnikov authored Jul 25, 2020
2 parents d8f9775 + 37eb200 commit c177abd
Showing 1 changed file with 42 additions and 25 deletions.
67 changes: 42 additions & 25 deletions Web/Controllers/JobsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ public IActionResult Wizard(int? id)
{
Categories = _categoriesService.GetAll(),
JobTypes = _hiretypesService.GetAll(),
Companies = _companiesService.GetByUserId(_currentUser.UserId)
Companies = _companiesService.GetByUserId(_currentUser.UserId)
};

if (id.HasValue)
{
var originalJob = _jobsService.GetById(id.Value);
if(originalJob.UserId == _currentUser.UserId)
if (originalJob.UserId == _currentUser.UserId)
{
model.Id = originalJob.Id;
model.CompanyId = originalJob.Company.Id;
Expand All @@ -98,7 +98,7 @@ public IActionResult Wizard(int? id)
model.LocationLongitude = originalJob.Location.Longitude;
}
else
{
{
return RedirectToAction("Index", "Home").WithError("No tienes permiso para editar esta posición");
}
}
Expand Down Expand Up @@ -146,7 +146,7 @@ public async Task<IActionResult> Wizard(WizardViewModel model)
if (model.Id.HasValue)
{
var originalJob = _jobsService.GetById(model.Id.Value);
if(originalJob.UserId == _currentUser.UserId)
if (originalJob.UserId == _currentUser.UserId)
{

originalJob.CategoryId = model.CategoryId;
Expand All @@ -157,8 +157,8 @@ public async Task<IActionResult> Wizard(WizardViewModel model)
originalJob.Title = model.Title;
originalJob.IsRemote = model.IsRemote;
originalJob.IsApproved = false;
if(originalJob.Location.PlaceId != model.LocationPlaceId)
{
if (originalJob.Location.PlaceId != model.LocationPlaceId)
{
originalJob.Location = new Location
{
PlaceId = model.LocationPlaceId,
Expand Down Expand Up @@ -230,7 +230,7 @@ public async Task<IActionResult> Wizard(WizardViewModel model)
}

}
catch(Exception ex)
catch (Exception ex)
{
HttpContext.RiseError(ex);
return View(model).WithError(ex.Message);
Expand All @@ -249,7 +249,8 @@ public async Task<IActionResult> Details(string Id, bool isPreview = false, bool
if (isLegacy)
{
var legacyJob = await _apiClient.GetJobById(Id);
if(legacyJob != null) {
if (legacyJob != null)
{
job = new Job()
{
Company = new Company()
Expand Down Expand Up @@ -279,7 +280,7 @@ public async Task<IActionResult> Details(string Id, bool isPreview = false, bool
}
else
{
job = this._jobsService.GetDetails(jobId, isPreview);
job = this._jobsService.GetDetails(jobId, isPreview);
}

if (job == null)
Expand All @@ -288,15 +289,31 @@ public async Task<IActionResult> Details(string Id, bool isPreview = false, bool
ViewBag.Title = job.Title;
ViewBag.Description = job.Description;
var viewModel = new JobDetailsViewModel
{
{
Job = job,
IsJobOwner = (job.UserId == _currentUser.UserId)
};

if(!isLegacy)
{
job.ViewCount++;
_jobsService.Update(job);
if (!isLegacy)
{
//Get the list of jobs visited in the cookie
//Format: comma separated jobs Id
//Naming: appname_meanfulname
var visitedJobs = Request.Cookies["empleado_visitedjobs"];

//If cookie value is null (not set) use empty string to avoid NullReferenceException
var visitedJobsList = (visitedJobs ?? string.Empty).Split(',', StringSplitOptions.RemoveEmptyEntries);

//If jobs has not be visited update ViewCount & add job Id to cookie
if (!visitedJobsList.Contains(Id))
{
job.ViewCount++;
_jobsService.Update(job);

visitedJobs = string.Join(",", visitedJobsList.Append(Id));
}

Response.Cookies.Append("empleado_visitedjobs", visitedJobs);
}

if (isPreview)
Expand Down Expand Up @@ -344,7 +361,7 @@ public JsonResult Hide(int id)
}
return Json(result);
}

[Authorize]
[HttpPost]
public JsonResult Delete(int id)
Expand All @@ -354,18 +371,18 @@ public JsonResult Delete(int id)
{
var job = _jobsService.GetById(id);

if(job == null)
if (job == null)
{
result.AddErrorMessage("No puedes eliminar un puesto que no existe.");
}
else if(job.UserId == _currentUser.UserId)
else if (job.UserId == _currentUser.UserId)
{
if (!job.IsActive)
{
result.AddErrorMessage("El puesto que intentas eliminar ya está eliminado.");
}
else
{
{
result = _jobsService.Delete(job);
}
}
Expand All @@ -374,7 +391,7 @@ public JsonResult Delete(int id)
result.AddErrorMessage("No puedes eliminar un puesto que no creaste.");
}
}
catch(Exception ex)
catch (Exception ex)
{
HttpContext.RiseError(ex);
result.AddErrorMessage(ex.Message);
Expand All @@ -395,8 +412,8 @@ public async Task Validate([FromForm] string payload)
try
{
var data = JsonConvert.DeserializeObject<PayloadResponseDto>(payload);
if(data == null)

if (data == null)
{
throw new Exception($"Payload is null, Body: {payload}");
}
Expand All @@ -415,11 +432,11 @@ public async Task Validate([FromForm] string payload)
await _slackService.PostJobResponse(jobOpportunity, Url, data.response_url, data?.user?.id, true);

try
{
var tweetText = jobOpportunity.Title + " " + Url.AbsoluteUrl("Details", "Jobs", new { Id=jobOpportunityId });
{
var tweetText = jobOpportunity.Title + " " + Url.AbsoluteUrl("Details", "Jobs", new { Id = jobOpportunityId });
await _twitterService.Tweet(tweetText);
}
catch(Exception tweetException)
catch (Exception tweetException)
{
HttpContext.RiseError(tweetException);
if (tweetException.InnerException != null)
Expand All @@ -446,7 +463,7 @@ public async Task Validate([FromForm] string payload)
catch (Exception ex)
{
HttpContext.RiseError(ex);
if(ex.InnerException != null)
if (ex.InnerException != null)
HttpContext.RiseError(ex.InnerException);
}
}
Expand Down

0 comments on commit c177abd

Please sign in to comment.