コード例 #1
0
        public async Task <IHttpActionResult> PutNoticeFile(int id, NoticeFile noticeFile)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != noticeFile.NoticeFileNo)
            {
                return(BadRequest());
            }

            db.Entry(noticeFile).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!NoticeFileExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
コード例 #2
0
        public async Task <IHttpActionResult> GetNoticeFile(int id)
        {
            NoticeFile noticeFile = await db.NoticeFiles.FindAsync(id);

            if (noticeFile == null)
            {
                return(NotFound());
            }

            return(Ok(noticeFile));
        }
コード例 #3
0
        public async Task <IHttpActionResult> PostNoticeFile(NoticeFile noticeFile)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.NoticeFiles.Add(noticeFile);
            await db.SaveChangesAsync();

            return(CreatedAtRoute("DefaultApi", new { id = noticeFile.NoticeFileNo }, noticeFile));
        }
コード例 #4
0
        public async Task <IHttpActionResult> DeleteNoticeFile(int id)
        {
            NoticeFile noticeFile = await db.NoticeFiles.FindAsync(id);

            if (noticeFile == null)
            {
                return(NotFound());
            }

            db.NoticeFiles.Remove(noticeFile);
            await db.SaveChangesAsync();

            return(Ok(noticeFile));
        }
コード例 #5
0
        public async Task <IHttpActionResult> PostNotice()
        {
            PetterResultType <Notice> petterResultType = new PetterResultType <Notice>();
            List <Notice>             notices          = new List <Notice>();
            List <NoticeFile>         noticeFiles      = new List <NoticeFile>();

            Notice notice = new Notice();

            if (Request.Content.IsMimeMultipartContent())
            {
                string folder = HostingEnvironment.MapPath(UploadPath.NoticePath);
                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))
                    {
                        NoticeFile noticeFile = new NoticeFile();
                        var        file       = await content.ReadAsByteArrayAsync();

                        string fileName = Utilities.additionFileName(content.Headers.ContentDisposition.FileName.Trim('"'));

                        if (!FileExtension.NoticeExtensions.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.NoticeWidth, FileSize.NoticeHeight, ImageFormat.Png);
                        noticeFile.FileName = fileName;
                        noticeFile.FilePath = UploadPath.NoticePath.Replace("~", "");

                        noticeFiles.Add(noticeFile);
                    }
                    else
                    {
                        string str = await content.ReadAsStringAsync();

                        string item = HttpUtility.UrlDecode(str);

                        #region switch case
                        switch (fieldName)
                        {
                        case "MemberNo":
                            notice.MemberNo = int.Parse(item);
                            break;

                        case "Title":
                            notice.Title = item;
                            break;

                        case "Content":
                            notice.Content = item;
                            break;

                        default:
                            break;
                        }
                        #endregion switch case
                    }
                }

                notice.StateFlag    = StateFlags.Use;
                notice.DateCreated  = DateTime.Now;
                notice.DateModified = DateTime.Now;

                // 공지게시판 등록
                db.Notices.Add(notice);
                int num = await this.db.SaveChangesAsync();

                // 공지게시판 파일 등록
                foreach (var item in noticeFiles)
                {
                    item.NoticeNo = notice.NoticeNo;
                }

                db.NoticeFiles.AddRange(noticeFiles);
                int num1 = await this.db.SaveChangesAsync();

                notices.Add(notice);
                petterResultType.IsSuccessful = true;
                petterResultType.JsonDataSet  = notices;
            }
            else
            {
                petterResultType.IsSuccessful = false;
                petterResultType.JsonDataSet  = null;
            }

            return(Ok(petterResultType));
        }