public async Task<IHttpActionResult> PutEventBoardFile(int id, EventBoardFile eventBoardFile) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != eventBoardFile.EvnetBoardFileNo) { return BadRequest(); } db.Entry(eventBoardFile).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EventBoardFileExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }
public async Task<IHttpActionResult> PostEventBoardFile(EventBoardFile eventBoardFile) { if (!ModelState.IsValid) { return BadRequest(ModelState); } db.EventBoardFiles.Add(eventBoardFile); await db.SaveChangesAsync(); return CreatedAtRoute("DefaultApi", new { id = eventBoardFile.EvnetBoardFileNo }, eventBoardFile); }
public async Task<IHttpActionResult> PutEventBoard(int id) { PetterResultType<EventBoard> petterResultType = new PetterResultType<EventBoard>(); List<EventBoard> eventBoards = new List<EventBoard>(); List<EventBoardFile> eventBoardFiles = new List<EventBoardFile>(); //EventBoardFile eventBoardFile = new Models.EventBoardFile(); //EventBoard eventBoard = new EventBoard(); if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } EventBoard eventBoard = await db.EventBoards.FindAsync(id); if (eventBoard == null) { return NotFound(); } if (Request.Content.IsMimeMultipartContent()) { string folder = HostingEnvironment.MapPath(UploadPath.EventBoardPath); Utilities.CreateDirectory(folder); var provider = await Request.Content.ReadAsMultipartAsync(); foreach (var content in provider.Contents) { string fieldName = content.Headers.ContentDisposition.Name.Trim('"'); if (!string.IsNullOrEmpty(content.Headers.ContentDisposition.FileName)) { EventBoardFile eventBoardFile = new EventBoardFile(); var file = await content.ReadAsByteArrayAsync(); string fileName = Utilities.additionFileName(content.Headers.ContentDisposition.FileName.Trim('"')); if (!FileExtension.EventBoardExtensions.Any(x => x.Equals(Path.GetExtension(fileName.ToLower()), StringComparison.OrdinalIgnoreCase))) { petterResultType.IsSuccessful = false; petterResultType.JsonDataSet = null; petterResultType.ErrorMessage = ResultErrorMessage.FileTypeError; return Ok(petterResultType); } string fullPath = Path.Combine(folder, fileName); File.WriteAllBytes(fullPath, file); string thumbnamil = Path.GetFileNameWithoutExtension(fileName) + "_thumbnail" + Path.GetExtension(fileName); Utilities.ResizeImage(fullPath, thumbnamil, FileSize.EventBoardWidth, FileSize.EventBoardHeight, ImageFormat.Png); // 이벤트게시판 대표 이미지 if (fieldName == FieldName.EventFieldName) { eventBoard.FileName = fileName; eventBoard.FilePath = UploadPath.EventBoardPath.Replace("~",""); } eventBoardFile.EventBoardNo = eventBoard.EventBoardNo; eventBoardFile.FileName = fileName; eventBoardFile.FilePath = UploadPath.EventBoardPath.Replace("~", ""); eventBoardFiles.Add(eventBoardFile); } else { string str = await content.ReadAsStringAsync(); string item = HttpUtility.UrlDecode(str); #region switch case switch (fieldName) { case "MemberNo": eventBoard.MemberNo = int.Parse(item); break; case "Title": eventBoard.Title = item; break; case "Content": eventBoard.Content = item; break; default: break; } #endregion switch case } } eventBoard.StateFlag = StateFlags.Use; eventBoard.DateModified = DateTime.Now; // 이벤트게시판 등록 db.Entry(eventBoard).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw; } // 이벤트게시판 파일 등록 db.EventBoardFiles.AddRange(eventBoardFiles); int num1 = await this.db.SaveChangesAsync(); eventBoards.Add(eventBoard); petterResultType.IsSuccessful = true; petterResultType.JsonDataSet = eventBoards; } else { petterResultType.IsSuccessful = false; petterResultType.JsonDataSet = null; } return Ok(petterResultType); }