public async Task <IActionResult> Create(TournamentVM tournamentVM)
        {
            if (ModelState.IsValid)
            {
                string image = "default_tournament_image.jpg";
                _fileManagementController = new FileManagementController(_appEnvironment);

                #region Image
                //if we uploaded an image
                if (tournamentVM.TournamentImage != null)
                {
                    //  #region File Upload Validation
                    bool fileUploadError = _fileManagementController.ValidateFileUploadExtensionAndSize(ModelState, tournamentVM.TournamentImage, FileType.Image, 2);
                    //return error if there is a file upload Error
                    if (fileUploadError)
                    {
                        TempData["message"] = NotificationSystem.AddMessage("لقد حصل خطأ في تحميل الملف", NotificationType.Danger.Value);
                        return(View(tournamentVM));
                    }
                    // upload and remove existing
                    image = _fileManagementController.UploadFile(tournamentVM.TournamentImage, "Media/Tournament");
                }
                #endregion

                // create tournament
                Tournament tournament = new Tournament()
                {
                    TournamentImage = image,
                    Name            = tournamentVM.Name,
                    Note            = tournamentVM.Note,
                    CreationDate    = DateTime.Now.Date,
                    IsActive        = true
                };

                _context.Add(tournament);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(tournamentVM));
        }
        public async Task <IActionResult> Create(OfficerVM officerVM, int TournamnetId)
        {
            // if has an error  on post i keep latest tournament
            var userId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;
            // tournament Id assigned to current user Id
            int        TournamentId = _context.UserTournaments.Where(u => u.UserId == userId).Select(t => t.TournamentId).FirstOrDefault();
            Tournament tournament   = _context.Tournaments.Where(t => t.Id == TournamentId).First(); officerVM.TournamentId = tournament.Id;

            officerVM.TournamentId   = tournament.Id;
            officerVM.TournamentName = tournament.Name;

            if (ModelState.IsValid)
            {
                using (var transaction = _context.Database.BeginTransaction())
                {
                    try
                    {
                        string image = "defaultofficer.png";
                        _fileManagementController = new FileManagementController(_appEnvironment);

                        #region Image
                        //if we uploaded an image
                        if (officerVM.Profileimage != null)
                        {
                            //  #region File Upload Validation
                            bool fileUploadError = _fileManagementController.ValidateFileUploadExtensionAndSize(ModelState, officerVM.Profileimage, FileType.Image, 2);
                            //return error if there is a file upload Error
                            if (fileUploadError)
                            {
                                TempData["message"] = NotificationSystem.AddMessage("لقد حصل خطأ في تحميل الملف", NotificationType.Danger.Value);
                                return(View(officerVM));
                            }
                            // upload and remove existing
                            image = _fileManagementController.UploadFile(officerVM.Profileimage, "Media/Officer_Profile");
                        }
                        #endregion

                        // create officer
                        Officer officer = new Officer()
                        {
                            TournamentId          = TournamnetId,
                            Name                  = officerVM.Name,
                            MilitaryNumber        = officerVM.MilitaryNumber,
                            PhoneNumber           = officerVM.PhoneNumber,
                            Email                 = officerVM.Email,
                            BirthAddress          = officerVM.BirthAddress,
                            BirthDate             = officerVM.BirthDate,
                            Nationality           = officerVM.Nationality,
                            Address               = officerVM.Address,
                            BloodType             = officerVM.BloodType,
                            Height                = officerVM.Height,
                            PreviousJob           = officerVM.PreviousJob,
                            HealthProblem         = officerVM.HealthProblem,
                            RatingEnteringYear    = officerVM.RatingEnteringYear,
                            RatingEnteringNumber  = officerVM.RatingEnteringNumber,
                            Specialization        = officerVM.Specialization,
                            RatingGradutionYear   = officerVM.RatingGradutionYear,
                            RatingGradutionNumber = officerVM.RatingGradutionNumber,
                            Notes                 = officerVM.Notes,
                            ProfileImage          = image,
                            IsActive              = true,
                            CreationDate          = DateTime.Now
                        };
                        _context.Officers.Add(officer);
                        _context.SaveChanges();



                        // loop each row in educational list then insert to database
                        foreach (EducationalAttainment item in officerVM.EducationalAttainments)
                        {
                            if (!String.IsNullOrEmpty(item.Certificate))
                            {
                                EducationalAttainment educationalAttainment = new EducationalAttainment()
                                {
                                    Certificate = item.Certificate,
                                    Source      = item.Source,
                                    Date        = item.Date,
                                    Grade       = item.Grade,
                                    OfficerId   = officer.Id
                                };
                                _context.EducationalAttainments.Add(educationalAttainment);
                            }
                        }

                        // loop each row in Language list then insert to database
                        foreach (Language item in officerVM.Languages)
                        {
                            if (!String.IsNullOrEmpty(item.Name))
                            {
                                Language language = new Language()
                                {
                                    Name      = item.Name,
                                    Listen    = item.Listen,
                                    Speak     = item.Speak,
                                    Read      = item.Read,
                                    Write     = item.Write,
                                    OfficerId = officer.Id
                                };
                                _context.Languages.Add(language);
                            }
                        }

                        // loop each row in Hobby list then insert to database
                        foreach (Hobby item in officerVM.Hobbies)
                        {
                            if (!String.IsNullOrEmpty(item.Name))
                            {
                                Hobby hobby = new Hobby()
                                {
                                    Name        = item.Name,
                                    Explanation = item.Explanation,
                                    Level       = item.Level,
                                    OfficerId   = officer.Id
                                };
                                _context.Hobbies.Add(hobby);
                            }
                        }

                        await _context.SaveChangesAsync();

                        transaction.Commit();
                        TempData["message"] = NotificationSystem.AddMessage("تم اضافة ضابط بنجاح", NotificationType.Success.Value);
                        return(RedirectToAction(nameof(Index)));
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        Console.WriteLine("Error occurred.");
                    }
                }
            }
            TempData["message"] = NotificationSystem.AddMessage("يجب التأكد من حقل الدورة قبل ادخال الضابط", NotificationType.Danger.Value);

            return(View(officerVM));
        }
Example #3
0
        public async Task <ActionResult> AdministrationDailyReportNote(DailyReportVM dailyReportVM, string SubmitButton)
        {
            DailyReport dailyReport = null;

            switch (SubmitButton)
            {
            case "search":
                // current user Id Logged In
                var userId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;
                // tournament Id assigned to current user Id
                int TournamentId = _context.UserTournaments.Where(u => u.UserId == userId).Select(t => t.TournamentId).FirstOrDefault();

                // get daily report by day posted
                dailyReport = _context.DailyReports.Where(t => t.TournamentId == dailyReportVM.TournamentId &&
                                                          (dailyReportVM.ReportDate.Date == t.ReportDate.Date)).FirstOrDefault();
                dailyReportVM.ReportId = dailyReport != null && dailyReport.Id != 0 ? dailyReport.Id : 0;

                // get daily incident by day posted
                dailyReportVM.DailyIncidents = _context.DailyIncidents.Where(t => t.StartDate.Value <= dailyReportVM.ReportDate && t.EndDate.Value >= dailyReportVM.ReportDate && t.TournamentId == TournamentId).Include(o => o.Officer).ToList();;
                // get daily note by day posted
                dailyReportVM.DailyNotes = _context.DailyNotes.Where(t => t.CreationDate == dailyReportVM.ReportDate && t.TournamentId == TournamentId).Include(o => o.Officer).ToList();
                return(View(dailyReportVM));

            case "delete":
                if (dailyReportVM.ReportId != 0)
                {
                    DailyReport dr = _context.DailyReports.Find(dailyReportVM.ReportId);


                    if (dailyReportVM.WantToDelete)
                    {
                        dr = _context.DailyReports.Find(dailyReportVM.ReportId);
                        _context.DailyReports.Remove(dr);
                        dailyReportVM.ReportId = 0;
                    }
                }
                if (dailyReportVM.DailyIncidents != null)
                {
                    foreach (var item in dailyReportVM.DailyIncidents)
                    {
                        if (item.WantToDelete)
                        {
                            _context.DailyIncidents.Remove(item);
                        }
                    }
                }
                if (dailyReportVM.DailyNotes != null)
                {
                    foreach (var item in dailyReportVM.DailyNotes)
                    {
                        if (item.WantToDelete)
                        {
                            _context.DailyNotes.Remove(item);
                        }
                    }
                }
                await _context.SaveChangesAsync();

                return(RedirectToAction("AdministrationDailyReportNote", new { currentdate = dailyReportVM.ReportDate }));
            }
            return(View(dailyReportVM));
        }