Example #1
0
        public async Task <IActionResult> AddAnnouncement(Announcement announcement, IFormFile file)
        {
            var userId    = _userManager.GetUserId(User);
            var imageName = await _announcementControllerService.UploadImages(file, userId);

            if (ModelState.IsValid & imageName != null)
            {
                announcement.DateAdded        = DateTime.Now.ToString("dd/MM/yyyy HH:mm");
                announcement.AplicationUserId = userId;

                _announcementContext.Announcements.Add(announcement);
                _announcementContext.SaveChanges();

                Image image = new Image();
                image.Name = imageName;
                AnnouncementImage announcementImage = new AnnouncementImage();
                _announcementContext.Images.Add(image);
                _announcementContext.SaveChanges();

                announcementImage.AnnouncementId = announcement.AnnouncementId;
                announcementImage.Image          = image;
                _announcementContext.AnnouncementImages.Add(announcementImage);
                _announcementContext.SaveChanges();

                return(RedirectToActionPermanent("Index", "Home"));
            }

            ViewBag.Message = "Please upload the photo";
            return(View(new AnnouncementListViewModel
            {
                Announcement = announcement,
                Categories = _categoryRepository.AllCategories
            }));
        }
        protected void btnAddAnnouncement_Click(object sender, EventArgs e)
        {
            if (AnnouncementImage.PostedFile != null && AnnouncementImage.PostedFile.FileName != "")
            {
                AnnouncementImage.SaveAs(Server.MapPath("Images/Organizations/" + AnnouncementImage.FileName.ToString()));
            }

            var announcement = new Models.OrganizationAnnouncement()
            {
                OrganizationId = Convert.ToInt32(OrganizationId.Value),
                Title          = announcementTitle.Value,
                Description    = announcementContent.Value,
                Venue          = announcementVenue.Value,
                StartDate      = DateTime.Parse(announcementStartDate.Value),
                StartTime      = announcementStartTime.Value,
                EndDate        = DateTime.Parse(announcementEndDate.Value),
                EndTime        = announcementEndTime.Value,
                ImagePath      = "Images/Organizations/" + AnnouncementImage.FileName.ToString()
            };

            announcement.Create();
        }
Example #3
0
        public int Save(Announcement announcement, string imageNamesSeparated)
        {
            if (imageNamesSeparated != null)
            {
                var imagesNames = imageNamesSeparated.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                var result      = new List <AnnouncementImage>();
                foreach (var s in imagesNames)
                {
                    var obj   = s.Split(';');
                    var image = new AnnouncementImage {
                        CreatedAt = DateTime.Now, Link = obj[0], IsTitular = false
                    };
                    if (obj.Length > 1)
                    {
                        image.IsTitular = obj[1] == "true";
                    }
                    result.Add(image);
                }

                announcement.Images = result;
            }

            return(Save(announcement));
        }