// PUT api/CalendarEventLocation/5
        public async Task<IHttpActionResult> PutCalendarEventLocation(int id, CalendarEventLocation calendarEventLocation)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != calendarEventLocation.CalendarEventLocationID)
            {
                return BadRequest();
            }
            if (!String.IsNullOrEmpty(calendarEventLocation.ReplacementFileName))
            {
                SaveFile(calendarEventLocation);
            }
            await repository.UpdateAsync(calendarEventLocation, calendarEventLocation.CalendarEventLocationID);

            return StatusCode(HttpStatusCode.NoContent);
        }
        public async Task<IHttpActionResult> PostCalendarEventLocation(CalendarEventLocation calendarEventLocation)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

			await repository.AddAsync(calendarEventLocation);

            if (!string.IsNullOrEmpty(calendarEventLocation.ReplacementFileName))
            {
                SaveFile(calendarEventLocation);
            }
            await repository.UpdateAsync(calendarEventLocation, calendarEventLocation.CalendarEventLocationID);

            return CreatedAtRoute("DefaultApi", new { id = calendarEventLocation.CalendarEventLocationID }, calendarEventLocation);
        }
 private void DeleteLayoutTypeFiles(CalendarEventLocation item)
 {
     DeleteFile(item.MapIconImage);
 }
        private void SaveFile(CalendarEventLocation item)
        {
            int id = item.CalendarEventLocationID;
            string uploadFolderName = "content/calendarEventLocations/" + id + "/";

            string uploadFolder = HostingEnvironment.MapPath("~/" + uploadFolderName);

            if (!Directory.Exists(uploadFolder))
            {
                Directory.CreateDirectory(uploadFolder);
            }

            string pictureName = item.Name.Clean();
            string extension = item.ReplacementFileName.Substring(item.ReplacementFileName.LastIndexOf('.'));

            if (item.MapIconImage != null)
            {
                DeleteLayoutTypeFiles(item);
            }

            //let's make sure alt is unique
            string newName = item.Name;
            GenerateUniqueName(uploadFolderName, uploadFolder, ref pictureName, extension, ref newName);
            item.Name = newName;

            //Retrieve file
            var file = System.Web.HttpContext.Current.Server.MapPath("~/Tmp/FileUploads/" + item.TemporaryFileName);

            //remove special characters from file name
            string fileName = pictureName + extension;

            string fullfilePath = Path.Combine(uploadFolder, fileName);

            File.Copy(file, fullfilePath);

            File.Delete(file);
            item.MapIconImage = uploadFolderName + fileName;
        }