Example #1
0
        public void CreateStudent(StudentCreateDto dto, int studiesId)
        {
            using var client = new SqlConnection(_configuration["ConnectionString"]);
            client.Open();
            using var transaction = client.BeginTransaction();
            try
            {
                var enrollmentId = GetEnrollmentByStudyIdAndSemester(studiesId, 1);

                if (!enrollmentId.HasValue)
                {
                    using var commandFindLatestId =
                              new SqlCommand("SELECT MAX([IdEnrollment]) FROM [Enrollment];", client, transaction);
                    var latestId = (int)commandFindLatestId.ExecuteScalar();

                    using var createEnrollmentCommand = new SqlCommand(@"
                        INSERT INTO [Enrollment](IdEnrollment, Semester, IdStudy, StartDate)
                        VALUES (@Id, 1, @IdStudy, GETDATE());
                    ", client, transaction);
                    createEnrollmentCommand.Parameters.Add(new SqlParameter("Id", latestId + 1));
                    createEnrollmentCommand.Parameters.Add(new SqlParameter("IdStudy", studiesId));

                    createEnrollmentCommand.ExecuteNonQuery();

                    enrollmentId = latestId + 1;
                }

                var dateSplitted = dto.BirthDate.Split('.');
                var day          = int.Parse(dateSplitted[0]);
                var month        = int.Parse(dateSplitted[1]);
                var year         = int.Parse(dateSplitted[2]);
                var parsedDate   = new DateTime(year, month, day);

                using var commandCreateStudent =
                          new SqlCommand(@"
                    INSERT INTO [Student]([IndexNumber],[FirstName],[LastName],[BirthDate],[IdEnrollment])
                    VALUES (@IndexNumber, @FirstName, @LastName, @BirthDate, @EnrolId);
                    ", client, transaction);
                commandCreateStudent.Parameters.AddRange(new []
                {
                    new SqlParameter("@IndexNumber", dto.IndexNumber),
                    new SqlParameter("@FirstName", dto.FirstName),
                    new SqlParameter("@LastName", dto.LastName),
                    new SqlParameter("@BirthDate", parsedDate),
                    new SqlParameter("@EnrolId", enrollmentId)
                });

                commandCreateStudent.ExecuteNonQuery();

                transaction.Commit();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
            }
            finally
            {
                client.Close();
            }
        }
        public async Task <string> CreateStudent(StudentCreateDto studentCreateDto)
        {
            var studentModel = _mapper.Map <Student>(studentCreateDto);

            _repository.CreateStudent(studentModel);
            var        studentModel1 = _mapper.Map <StudentReadDto>(studentModel);
            HttpClient client        = new HttpClient();

            //ADD your ngrok url
            client.BaseAddress = new Uri("https://3a5e06b6ad12.ngrok.io");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            //GET Method
            var body = JsonConvert.SerializeObject(studentModel1, Formatting.Indented, new JsonSerializerSettings
            {
                PreserveReferencesHandling = PreserveReferencesHandling.All
            });
            var content = new StringContent(body, Encoding.UTF8, "application/json");
            HttpResponseMessage response = await client.PostAsync("api/notify", content);

            if (response.IsSuccessStatusCode)
            {
                // Get the URI of the created resource.
                Uri returnUrl = response.Headers.Location;
                Console.WriteLine(returnUrl);
            }
            var studentReadDto = _mapper.Map <StudentReadDto>(studentModel);

            return("success");
        }
Example #3
0
        public IActionResult Post(StudentCreateDto dto)
        {
            if (string.IsNullOrWhiteSpace(dto.IndexNumber) ||
                string.IsNullOrWhiteSpace(dto.FirstName) ||
                string.IsNullOrWhiteSpace(dto.LastName) ||
                string.IsNullOrWhiteSpace(dto.BirthDate) ||
                !new Regex("^\\d{1,2}\\.\\d{1,2}\\.\\d{4}$").IsMatch(dto.BirthDate))
            {
                return(BadRequest("Format danych jest nieprawidłowy"));
            }

            var studiesId = _dbService.GetStudiesIdByName(dto.Studies);

            if (!studiesId.HasValue)
            {
                return(BadRequest($"Nie znaleziono studiów o nazwie = {dto.Studies}"));
            }

            if (!_dbService.IsIndexNumberUnique(dto.IndexNumber))
            {
                return(BadRequest("Student o podanym id już istnieje"));
            }

            _dbService.CreateStudent(dto, studiesId.Value);

            return(Created("", ""));
        }
Example #4
0
        public async Task <StudentDto> Create(StudentCreateDto student)
        {
            var studentModel = student.Adapt <Student>();

            var newStudent = await this.studentRepository.Create(studentModel);

            await this.AddRelatedEntities(newStudent);

            return(newStudent.Adapt <StudentDto>());
        }
Example #5
0
        public ActionResult <StudentReadDto> CreateStudent(StudentCreateDto studentCreateDto)
        {
            var studentModel = _mapper.Map <Student>(studentCreateDto);

            _repository.CreateStudent(studentModel);
            _repository.SaveChanges();

            var studentReadDto = _mapper.Map <StudentReadDto>(studentModel);

            return(CreatedAtRoute(nameof(GetStudentById), new { StudentID = studentReadDto.StudentId }, studentReadDto));
        }
Example #6
0
        public ActionResult <StudentReadDto> CreateStudent(StudentCreateDto std)
        {
            Student StudentModel = _mapper.Map <Student>(std);

            _repository.CreateStudent(StudentModel);
            _repository.SaveChanges();

            StudentReadDto _StudentReadDto = _mapper.Map <StudentReadDto>(StudentModel);

            return(CreatedAtRoute(nameof(GetStudentById), new { id = _StudentReadDto.id }, _StudentReadDto));
        }
Example #7
0
        public ActionResult <StudentReadDto> CreateCommand(StudentCreateDto student)
        {
            var commandModel = _mapper.Map <Student>(student);

            _repository.CreateStudent(commandModel);
            _repository.SaveChanges();

            var commandReadDto = _mapper.Map <StudentReadDto>(commandModel);

            return(CreatedAtRoute(nameof(GetStudentById), new { Id = commandReadDto.Id }, commandReadDto));
        }
        public ActionResult CreateStudent(StudentCreateDto StudentCreateDto)
        {
            var StudentModel = _mapper.Map <User>(StudentCreateDto);

            _repo.CreateStudent(StudentModel);
            _repo.SaveChanges();

            return(NoContent());

            // return Ok(StudentModel);
        }
Example #9
0
        public virtual async Task <StudentDto> CreateAsync(StudentCreateDto input)
        {
            var newStudent = ObjectMapper.Map <StudentCreateDto, Student>(input);


            newStudent.TenantId = CurrentTenant.Id;


            var student = await _studentRepository.InsertAsync(newStudent);

            await CurrentUnitOfWork.SaveChangesAsync();

            return(ObjectMapper.Map <Student, StudentDto>(student));
        }
Example #10
0
        public void CreateStudent(StudentCreateDto dto, int studiesId)
        {
            using var transaction = _context.Database.BeginTransaction();

            try
            {
                var enrollmentId = GetEnrollmentByStudyIdAndSemester(studiesId, 1);

                if (!enrollmentId.HasValue)
                {
                    var latest = _context.Enrollments.Max(e => e.IdEnrollment);

                    _context.Enrollments.Add(new Enrollment
                    {
                        IdEnrollment = latest + 1,
                        Semester     = 1,
                        IdStudy      = studiesId,
                        StartDate    = DateTime.Now
                    });

                    enrollmentId = latest + 1;
                }

                var dateSplitted = dto.BirthDate.Split('.');
                var day          = int.Parse(dateSplitted[0]);
                var month        = int.Parse(dateSplitted[1]);
                var year         = int.Parse(dateSplitted[2]);
                var parsedDate   = new DateTime(year, month, day);

                _context.Students.Add(new Student()
                {
                    IndexNumber  = dto.IndexNumber,
                    FirstName    = dto.FirstName,
                    LastName     = dto.LastName,
                    BirthDate    = parsedDate,
                    IdEnrollment = enrollmentId.Value
                });

                _context.SaveChanges();
                transaction.Commit();
            }
            catch (Exception)
            {
                transaction.Rollback();
            }
        }
Example #11
0
        public ActionResult UpdateStudent(int id, StudentCreateDto studentCreateDto)
        {
            var student = _repository.GetStudentById(id);

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

            _mapper.Map(studentCreateDto, student);

            _repository.UpdateStudent(student);

            _repository.SaveChanges();

            return(NoContent());
        }
        public IActionResult CreateStudent(StudentCreateDto StudentCreateDto)
        {
            if (StudentCreateDto == null)
            {
                return(BadRequest(ModelState));
            }

            if (_studentRepo.isStudentExists(StudentCreateDto.Name))
            {
                ModelState.AddModelError("", $"Teacher is exists!");
                return(StatusCode(404, ModelState));
            }

            var studentObj = _mapper.Map <Student>(StudentCreateDto);

            if (!_studentRepo.CreateStudent(studentObj))
            {
                ModelState.AddModelError("", $"Something went wrong when saving the record {studentObj.Name}");
                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute(nameof(GetStudent), new { studentId = studentObj.Id }, studentObj));
        }
Example #13
0
 public Task <StudentWithDetailsDto> CreateAsync(StudentCreateDto StudentCreate)
 {
     return(this.StudentCrudAppService.CreateAsync(StudentCreate));
 }
 public async Task <StudentDto> Create([FromBody] StudentCreateDto student)
 {
     return(await this.studentServices.Create(student));
 }