public async Task CreateAsync(CreateJobInputModel input, string userId, string imagePath) { var job = new Job { Name = input.Name, JobMainCategory = input.JobMainCategory, Town = input.Town, Description = input.Description, Reward = input.Reward, StartDate = input.StartDate, EndDate = input.EndDate, JobSubCategoryId = input.JobSubCategoryId, UserId = userId, }; var allowedExtentions = new[] { "jpg", "png", "gif" }; Directory.CreateDirectory($"{imagePath}/jobs/"); foreach (var image in input.Images) { var extension = Path.GetExtension(image.FileName).TrimStart('.'); if (!allowedExtentions.Any(x => extension.EndsWith(x))) { throw new Exception($"Invalid image format {extension}"); } var dbImage = new JobImage { // UserId = userId, Extension = extension, }; job.JobImages.Add(dbImage); var phisicalPath = $"{imagePath}/jobs/{dbImage.Id}.{extension}"; using Stream fileStream = new FileStream(phisicalPath, FileMode.Create); await image.CopyToAsync(fileStream); } await this.jobsRepository.AddAsync(job); await this.jobsRepository.SaveChangesAsync(); }
public async Task <ActionResult> Create(PostJobViewModel obj, HttpPostedFileBase file) { string categorypass = null; string skillpass = null; if (!ModelState.IsValid) { return(View(obj)); } var pn = new Job { JobDetails = obj.JobDetails, ExpireDate = obj.ExpireDate, Id = SqlGuidUtil.NewSequentialId(), IsNotification = obj.IsNotification, Lan = obj.Lan, Lat = obj.Lat, PostedDate = obj.PostedDate, Radius = obj.Radius, JobTitle = obj.JobTitle, CompanyAddress = obj.CompanyAddress, CompanyJobId = obj.CompanyJobId, CompanyName = obj.CompanyName, MonthExperience = obj.MonthExperience, Website = obj.Website, YearExpereince = obj.YearExpereince, IsApplicableforWholeCity = obj.IsApplicableforWholeCity }; if (obj.SelectedCategory != null) { // Category foreach (string equipme in obj.SelectedCategory) { categorypass = categorypass + "'" + equipme + "',"; var postcategory = new JobCategory(); postcategory.Id = SqlGuidUtil.NewSequentialId(); postcategory.CategoryId = Guid.Parse(equipme); pn.JobCategories.Add(postcategory); } } if (obj.SelectedSkill != null) { // Category foreach (string skill in obj.SelectedSkill) { skillpass = skillpass + "'" + skill + "',"; var postskill = new JobSkill(); postskill.JobSkillId = SqlGuidUtil.NewSequentialId(); postskill.SkillId = Guid.Parse(skill); pn.JobSkills.Add(postskill); } } if (file != null && file.ContentLength > 0) { var ni = new JobImage { Id = SqlGuidUtil.NewSequentialId() }; // Initialize variables string sSavePath; // Set constant values sSavePath = "Images/"; if (!Directory.Exists(Server.MapPath("~/" + sSavePath))) { // if not created then it will create it. Directory.CreateDirectory(Server.MapPath("~/" + sSavePath)); } // Make sure a duplicate file doesn’t exist. If it does, keep on appending an // incremental numeric until it is unique var sFilename = Path.GetFileName(file.FileName); var fileExtension = Path.GetExtension(file.FileName); var fileAppend = 0; if (!string.IsNullOrWhiteSpace(sFilename)) { while (System.IO.File.Exists(Server.MapPath(sSavePath + sFilename))) { fileAppend++; sFilename = Path.GetFileNameWithoutExtension(file.FileName) + fileAppend.ToString() + fileExtension; } // Save document file.SaveAs(Server.MapPath("~/" + sSavePath + sFilename)); ni.ImagePath = "/" + sSavePath + sFilename; pn.JobImages.Add(ni); } } // Then create: _db.Jobs.Add(pn); await _db.SaveChangesAsync(); // If is Notification, then only send push notification if (!pn.IsNotification) { return(RedirectToAction("Index")); } IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler(); scheduler.Start(); decimal radiust = Convert.ToDecimal(pn.Radius) * 1000; TimeSpan span = pn.ExpireDate - pn.PostedDate; //JobDataMap jdm = new JobDataMap(); if (categorypass != null) { categorypass = categorypass.Remove(categorypass.Length - 1); //jdm.Add("category", categorypass); } //jdm.Add("idd", pn.Id); //jdm.Add("lat", pn.Lat); //jdm.Add("lng", pn.Lan); //jdm.Add("title", pn.Title); //jdm.Add("description", pn.Description); //jdm.Add("expirationdate", pn.ExpireDate.ToString(CultureInfo.InvariantCulture)); //jdm.Add("radius", radiust.ToString("#.##")); //jdm.Add("millisecond", span.TotalMilliseconds.ToString(CultureInfo.InvariantCulture)); IJobDetail job = JobBuilder.Create <PushNewsScheduleJob>() .UsingJobData("category", categorypass) .UsingJobData("skills", skillpass) .UsingJobData("idd", pn.Id.ToString()) .UsingJobData("lat", pn.Lat) .UsingJobData("lng", pn.Lan) .UsingJobData("title", pn.JobTitle) .UsingJobData("description", pn.JobDetails) .UsingJobData("years", pn.YearExpereince) .UsingJobData("months", pn.MonthExperience) .UsingJobData("expirationdate", pn.ExpireDate.ToString()) .UsingJobData("radius", radiust.ToString("#.##")) .UsingJobData("millisecond", span.TotalMilliseconds.ToString()) .Build(); ITrigger trigger = TriggerBuilder.Create() .StartNow() .Build(); scheduler.ScheduleJob(job, trigger); //List<UserRegId> urlist = await _db.UserRegIds.Where(ur => ur.UserId = ).ToListAsync(); //foreach(UserRegId u in urlist) //{ // PushNewsModel pnm = new PushNewsModel(); // pnm.id = pn.Id.ToString(); // pnm.lat = pn.Lat; // pnm.lng = pn.Lan; // pnm.title = pn.Title; // pnm.description = pn.Description; // pnm.expirationdate = pn.ExpireDate.ToString(); // decimal radiust = pn.Radius * 1000; // pnm.radius = radiust.ToString("#.##"); // TimeSpan span = pn.ExpireDate - pn.PostedDate; // pnm.millisecond = span.TotalMilliseconds.ToString(); // SendNotification(u.RegId,pnm); //} return(RedirectToAction("Index")); }