public void Execute(JobImageDto request) { _validator.ValidateAndThrow(request); //ValidationException var jobImage = new JobImages { ImagePath = request.ImagePath, JobId = request.JobId }; _context.Entry(jobImage).State = jobImage.Id > 0 ? EntityState.Modified : EntityState.Added; _context.SaveChanges(); }
public ActionResult Put(int id, [FromBody] JobUpdate value) { Console.WriteLine("Updating existing Job: " + id); // Users name (it's actually an email) - for this to work in IdentityServer in the ApiClaims must be defined name (and email) var jwtuser = User.Claims.Where(x => x.Type == "name").FirstOrDefault(); Console.WriteLine("Authenticated user name is: " + jwtuser.Value); //it's in a {key: value} format var userName = jwtuser.Value; // Retrieve objects related to the received id and username //var user = _context.Users.FirstOrDefault(u => u.Username == userName); //var job = _context.Jobs.FirstOrDefault(j => j.Id == id & j.ClientId == user.Id); var job = (from j in _context.Jobs join u in _context.Users on j.ClientId equals u.Id where u.Username == userName && j.Id == id select j).SingleOrDefault(); var jobDetails = _context.JobDetails.FirstOrDefault(jd => jd.JobId == id); var oldImages = _context.JobImages.Where(i => i.JobId == id); // this needs to consider 2 things: update new images and remove deleted, skipping this for now // Procede with update if the job id exist if (job != null) { // Update Job record fields job.JobTitle = value.jobTitle; job.RatePerHour = value.ratePerHour; job.RateFixed = value.rateFixed; job.DurationDays = value.durationDays; job.DurationHours = value.durationHours; //job.LocationPostCode = value.locationPostCode; job.ContactTelephone1 = value.contactTelephone1; job.ContactTelephone2 = value.contactTelephone2; //job.ContactEmail = value.contactEmail; job.JobState = value.jobState; job.PlannedStartDate = value.plannedStartDate; job.PlannedFinishDate = value.plannedFinishDate; job.LocationLat = value.locationLat; job.LocationLng = value.locationLng; job.SkillId = ConvertSkillNameToSkillID(value.skill); _context.SaveChanges(); // Update JobDetails record fields jobDetails.JobDescription = value.jobDescription; _context.SaveChanges(); // Update Images records if (value.images.Length != 0) { Console.WriteLine("Number of images in the array: " + value.images.Length); // Delete existing links from the database if (oldImages != null) { foreach (var oldImage in oldImages) { _context.JobImages.Remove(oldImage); // add a logic to record deleted image Urls, for cleaning up S3 } _context.SaveChanges(); } // Create new links of the latest images foreach (var image in value.images) { Console.WriteLine("-- New image: " + image); var newImages = new JobImages { JobId = job.Id, // Use Identity value of the inserted record to the Jobs table Url = image }; _context.JobImages.Add(newImages); _context.SaveChanges(); } } else { Console.WriteLine("No images were sent in the POST"); } } else { Console.WriteLine("Job ID: " + id + " for the user " + userName + " does not exist"); return(NotFound()); } return(Ok()); }
public string Post([FromBody] NewJob value) { // Users name (it's actually an email) - for this to work in IdentityServer in the ApiClaims must be defined name (and email) var jwtuser = User.Claims.Where(x => x.Type == "name").FirstOrDefault(); Console.WriteLine("Authenticated user name is: " + jwtuser.Value); //it's in a {key: value} format var userName = jwtuser.Value; var userDetails = _context.Users.Where(u => u.Username == jwtuser.Value).FirstOrDefault(); // !!!!! NOTE - all the inserts must be in one SQL transaction // Insert into the Jobs table var newJob = new Jobs { ClientId = userDetails.Id, JobTitle = value.JobTitle, RatePerHour = value.RatePerHour, RateFixed = value.RateFixed, DurationDays = value.DurationDays, DurationHours = value.DurationHours, //LocationPostCode = value.LocationPostCode, ContactTelephone1 = value.ContactTelephone1, ContactTelephone2 = value.ContactTelephone2, //ContactEmail = value.ContactEmail, JobState = value.JobState, // Initial state just after creation PlannedStartDate = value.PlannedStartDate, PlannedFinishDate = value.PlannedFinishDate, LocationLat = value.LocationLat, LocationLng = value.LocationLng, SkillId = ConvertSkillNameToSkillID(value.Skill) }; _context.Jobs.Add(newJob); _context.SaveChanges(); // Insert into the JobDetails table var newJobDetails = new JobDetails { JobId = newJob.Id, // Use Identity value of the inserted record to the Jobs table JobDescription = value.JobDescription }; _context.JobDetails.Add(newJobDetails); _context.SaveChanges(); // Insert into the Images table (not the most efficient way, fix later) if (value.Images != null) { foreach (var image in value.Images) { Console.WriteLine("--" + image); var newImages = new JobImages { JobId = newJob.Id, // Use Identity value of the inserted record to the Jobs table Url = image }; _context.JobImages.Add(newImages); _context.SaveChanges(); } } //return new job id back to Angular; return(newJob.Id.ToString()); // Return error //Console.WriteLine("--New Job creation FAILED"); //this.HttpContext.Response.StatusCode = 400; //return null; }