public async Task<IHttpActionResult> CreateProject(ProjectPostDTO project_data) { string reg = User.Identity.GetUserId(); Company owner = await db.Companies.Where(d => d.registrationId == reg).SingleOrDefaultAsync(); if (owner == null) { return StatusCode(HttpStatusCode.NotAcceptable); } int owner_id = owner.Id; if (owner==null) { return StatusCode(HttpStatusCode.NotAcceptable); } Int32 timeStamp ; Int32 closing; DateTime dueDate; try { timeStamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; dueDate = DateTime.ParseExact(project_data.due_date, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture).ToUniversalTime(); closing = (Int32)(dueDate.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; } catch (Exception e) { return StatusCode(HttpStatusCode.ExpectationFailed); } TimeSpan span = dueDate.Subtract(DateTime.UtcNow); if (span.Days<1) { return StatusCode(HttpStatusCode.Forbidden); } Project project = new Project() { title = project_data.project_title, detailsText = project_data.description, audience = project_data.targeted_level, category = project_data.project_category, companyId = owner_id, dueDate = project_data.due_date, currentDate = timeStamp, closingDate = closing, launchDate = "" + DateTime.UtcNow.Month + "/" + DateTime.UtcNow.Day + "/" + DateTime.UtcNow.Year, other2 = project_data.city, other1 = project_data.remuneration }; if (!ModelState.IsValid) { return BadRequest(ModelState); } Project posted = db.Projects.Add(project); await db.SaveChangesAsync(); ProjectDetailDTO posted_project = new ProjectDetailDTO() { project_id = posted.Id, project_title = posted.title, project_category = posted.category, project_status = posted.status, description = posted.detailsText, attachment = posted.detailsResourceUrl, launch_date = posted.launchDate, due_date = posted.dueDate, targeted_level = posted.audience, num_views = posted.numViews, num_application = posted.numApplication, num_comments = posted.numComments, city = posted.other2, remuneration = posted.other1, company = new CompanyDTO() { companyID = posted.company.Id, name = posted.company.companyName, company_category = posted.company.category, profile_pic = posted.company.profilePicture, wall_pic = posted.company.wallpaper } }; return Ok(posted_project); // return StatusCode(HttpStatusCode.Created); }
public async Task<IHttpActionResult> EditProject(int id, ProjectEditDTO project_data) { string reg = User.Identity.GetUserId(); if (!ModelState.IsValid) { return BadRequest(ModelState); } if (!ProjectExists(id)) { return StatusCode(HttpStatusCode.NotFound); } Company company = await db.Companies.FirstAsync(b => b.registrationId == reg); int company_id = company.Id; Project project = new Project() { Id = id, companyId = company_id, title = project_data.project_title, detailsText = project_data.description, detailsResourceUrl = project_data.attachment, audience = project_data.targeted_level, category = project_data.project_category, status = "open", dueDate = project_data.due_date, other1 = project_data.remuneration, other2 = project_data.city }; if (project_data.due_date != null) { Int32 timeStamp; Int32 closing; DateTime dueDate; try { timeStamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; dueDate = DateTime.ParseExact(project_data.due_date, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture).ToUniversalTime(); closing = (Int32)(dueDate.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; TimeSpan span = dueDate.Subtract(DateTime.UtcNow); if (span.Days < 1) { return StatusCode(HttpStatusCode.Forbidden); } else { project.closingDate = closing; } } catch (Exception e) { return StatusCode(HttpStatusCode.ExpectationFailed); } } Project original_project = await db.Projects.FindAsync(id); if (original_project.company.Id != company_id) { return StatusCode(HttpStatusCode.Forbidden); } foreach (PropertyInfo propertyInfo in original_project.GetType().GetProperties()) { if (propertyInfo.GetValue(project, null) == null) propertyInfo.SetValue(project, propertyInfo.GetValue(original_project, null), null); } try { db.Entry(original_project).CurrentValues.SetValues(project); await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProjectExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.OK); }