public IHttpActionResult PutCompany(int id, Company company) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != company.Id) { return(BadRequest()); } db.Entry(company).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!CompanyExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <HttpResponseMessage> UploadUserPhoto(int id) { loggedInUser = GetLoggedInUser(); Entity.Models.User userEntity = db.Users.FirstOrDefault(u => u.CompanyId == loggedInUser.CompanyId && u.Id == id); if (userEntity == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } // Check if the request contains multipart/form-data. if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } string root = HttpContext.Current.Server.MapPath("~/App_Data"); var provider = new MultipartFormDataStreamProvider(root); try { // Read the form data. await Request.Content.ReadAsMultipartAsync(provider); // This illustrates how to get the file names. foreach (MultipartFileData file in provider.FileData) { Trace.WriteLine(file.Headers.ContentDisposition.FileName); Trace.WriteLine("Server file path: " + file.LocalFileName); // TODO - should be async read/write var info = new FileInfo(file.LocalFileName); userEntity.Photo = File.ReadAllBytes(info.FullName); db.SaveChanges(); } return(Request.CreateResponse(HttpStatusCode.OK)); } catch (System.Exception e) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e)); } }
public IHttpActionResult UpdateProperty(int id, Dto.Models.Property propertyDto) { loggedInUser = GetLoggedInUser(); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var result = new PropertyValidator().Validate(propertyDto); if (!result.IsValid) { return(new ValidatorError("Validation failed for updated property DTO", HttpStatusCode.BadRequest, result, Request)); } if (id != propertyDto.Id) { return(new BadRequestErrorMessageResult("Updated property DTO id mismatch", this)); } if (propertyDto.CompanyId != loggedInUser.CompanyId) { return(BadRequest("Updated property does not belong to same company as logged in user")); } else if (db.Properties.Count(p => p.CompanyId == loggedInUser.CompanyId && p.Id != propertyDto.Id && p.Name == propertyDto.Name) > 0) { return(new BadRequestErrorMessageResult("Another property has the same name as this property", this)); } var propertyEntity = Mapper.Map <Dto.Models.Property, Entity.Models.Property>(propertyDto); db.Properties.Attach(propertyEntity); db.Entry(propertyEntity).State = EntityState.Modified; if (propertyDto.Users != null) { // Update Users for Property db.Entry(propertyEntity).Collection(u => u.Users).Load(); // force load var userIdList = propertyDto.Users.Select(u => u.Id); var newUsers = db.Users.Where(u => userIdList.Contains(u.Id)).ToList(); propertyEntity.Users = newUsers; // for this to work, existing Users must have been forced loaded. } try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!PropertyExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }