Example #1
0
        public IActionResult UpdateSection([FromBody] Section section)
        {
            var lang          = Request.Headers["language"].ToString();
            var errorMessages = new List <string>();

            try
            {
                var sec = _sectionRepository.FindById(section.Id);
                sec.Name_EN   = section.Name_EN;
                sec.Name_FR   = section.Name_FR ?? section.Name_EN;
                sec.Slug_EN   = new SlugHelper().GenerateSlug(section.Name_EN);
                sec.Slug_FR   = section.Name_FR != null ? new SlugHelper().GenerateSlug(section.Name_FR) : new SlugHelper().GenerateSlug(section.Name_EN);
                sec.UpdatedAt = DateTime.Now;
                sec.UpdatedBy = section.UpdatedBy;

                Section updatedOldSection = null;

                if (sec.Order != section.Order)
                {
                    var oldOrder = sec.Order;

                    // Previous
                    var oldSec = _sectionRepository.GetSectionsByCourseId(sec.Course.Id)
                                 .SingleOrDefault(s => s.Order == section.Order);

                    if (oldSec != null)
                    {
                        oldSec.Order      = oldOrder;
                        updatedOldSection = _sectionRepository.Update(oldSec);
                    }

                    // New
                    sec.Order = section.Order;
                }

                var updatedSection = _sectionRepository.Update(sec);

                if (updatedOldSection != null)
                {
                    return(Ok(new { updatedSection, updatedOldSection }));
                }
                else
                {
                    return(Ok(new { updatedSection }));
                }
            }
            catch
            {
                errorMessages.Add(_translator.GetTranslation("ERROR", lang));
                return(BadRequest(new { errors = errorMessages }));
            }
        }
Example #2
0
        public ExampleSection Find(int id)
        {
            var entity = _sectionRepository.FindById(id);

            if (entity == null)
            {
                return(null);
            }

            var section = new ExampleSection(entity, true);

            if (entity.Topics != null && entity.Topics.Any())
            {
                section.Topics = entity.Topics.Select(t => new ExampleTopic(t, section)).ToList();
            }
            else
            {
                section.Topics = new List <ExampleTopic>();
            }

            return(section);
        }
Example #3
0
        public IActionResult CreateSession([FromBody] Session session)
        {
            var lang          = Request.Headers["language"].ToString();
            var errorMessages = new List <string>();

            var section = _sectionRepository.FindById(session.Section.Id);

            try
            {
                var newSession = new Session()
                {
                    Section   = section,
                    Title_EN  = session.Title_EN,
                    Title_FR  = session.Title_FR ?? session.Title_EN,
                    Duration  = session.Duration,
                    Slug_EN   = new SlugHelper().GenerateSlug(session.Title_EN),
                    Slug_FR   = session.Title_FR != null ? new SlugHelper().GenerateSlug(session.Title_FR) : new SlugHelper().GenerateSlug(session.Title_EN),
                    Order     = session.Order,
                    CreatedAt = DateTime.Now,
                    UpdatedAt = DateTime.Now,
                    CreatedBy = session.CreatedBy,
                    UpdatedBy = session.UpdatedBy,
                    DeletedAt = null,
                    DeletedBy = null
                };

                var createdSession = _sessionRepository.Create(newSession);

                return(Ok(new { createdSession }));
            }
            catch
            {
                errorMessages.Add(_translator.GetTranslation("ERROR", lang));
                return(BadRequest(new { errors = errorMessages }));
            }
        }
        public IActionResult CreateSession([FromBody] Session session, [FromQuery] string action)
        {
            var lang = Request.Headers["language"].ToString();
            var errorMessages = new List<string>();

            try
            {
                var section = _sectionRepository.FindById(session.Section.Id);

                if (action == "add")
                {
                    var newSession = new Session()
                    {
                        Section = section,
                        Title_EN = session.Title_EN,
                        Slug_EN = new SlugHelper().GenerateSlug(session.Title_EN),
                        Order = session.Order,
                        Duration = session.Duration,
                        CreatedAt = DateTime.Now,
                        UpdatedAt = DateTime.Now,
                        CreatedBy = session.CreatedBy,
                        UpdatedBy = session.UpdatedBy
                    };

                    _sessionRepository.Create(newSession);
                }
                else if (action == "edit")
                {
                    var s = _sessionRepository.FindById(session.Id);
                    s.Title_EN = session.Title_EN;
                    s.Slug_EN = new SlugHelper().GenerateSlug(session.Title_EN);
                    s.Duration = session.Duration;
                    s.UpdatedAt = DateTime.Now;
                    s.UpdatedBy = session.UpdatedBy;

                    if (s.Order != session.Order)
                    {
                        var oldOrder = s.Order;

                        // Previous
                        var oldSession = section.Sessions.SingleOrDefault(s => s.Order == session.Order);
                        if (oldSession != null)
                        {
                            oldSession.Order = oldOrder;
                            var updatedOldSession = _sessionRepository.Update(oldSession);
                        }

                        // New
                        s.Order = session.Order;

                    }

                    _sessionRepository.Update(s);
                }
                var course = _courseRepository.FindById(session.Section.Course.Id);

                var response = ResponseGenerator.GenerateCourseResponse(course, true);

                return Ok(new { updatedCourse = response });
            }
            catch 
            {
                errorMessages.Add(_translator.GetTranslation("ERROR", lang));
                return BadRequest(new { errors = errorMessages });
            }
        }
Example #5
0
 public async Task <Section> FindById(int id)
 {
     return(await _sectionRepository.FindById(id));
 }