public async Task <IActionResult> AddStudentAsync(AddStudentDTO addStudentDto) { if (ModelState.IsValid) { try { var studentModel = _mapper.Map <Student>(addStudentDto); studentModel.RegisteredDate = DateTime.Now; await _context.Add(studentModel); var readStudentDto = _mapper.Map <ReadStudentDTO>(studentModel); _log.AddLog(Request, _httpContextAccessor, this.ControllerContext.RouteData.Values["controller"].ToString(), this.ControllerContext.RouteData.Values["action"].ToString(), "Eshte shtuar nje student i ri"); return(Ok(new DataMessage("Studenti eshte regjistruar me sukses!"))); } catch (Exception ex) { _log.AddLog(Request, _httpContextAccessor, this.ControllerContext.RouteData.Values["controller"].ToString(), this.ControllerContext.RouteData.Values["action"].ToString(), "Deshtim ne regjistrimin e nje studenti"); return(BadRequest(new DataMessage("Regjistrimi deshtoi " + ex.Message))); } } else { return(BadRequest(ModelState)); } }
public IHttpActionResult AddStudent(int id, AddStudentViewModel student) { AddStudentDTO newStudent = new AddStudentDTO(); newStudent.TemplateID = student.TemplateID; newStudent.Semester = student.Semester; newStudent.SSN = student.SSN; try { _context.AddStudentToCourse(newStudent); var location = Url.Link("StudentInCourse", new { id = id }); return(Created(location, student.SSN)); } catch (Exception ex) { if (ex is KeyNotFoundException) { return(NotFound()); } else { return(InternalServerError()); } } }
/// <summary> /// Maps <paramref name="addStudentDTO"/> to <c><b>Student</b></c> object and adds that product to repository. /// </summary> /// <param name="addStudentDTO">Student to be added.</param> /// <returns></returns> public async Task AddStudentAsync(AddStudentDTO addStudentDTO) { var appUser = new AppUser { UserName = addStudentDTO.UserName, Email = addStudentDTO.Email, PhoneNumber = addStudentDTO.PhoneNumber, Student = new Student { Name = addStudentDTO.Name, Surname = addStudentDTO.Surname, University = addStudentDTO.University, Level = addStudentDTO.Level, Age = addStudentDTO.Age, Dream = addStudentDTO.Dream, HomeAddress = addStudentDTO.HomeAddress, MentorThoughts = addStudentDTO.MentorThoughts, IsConfidentialityAgreementSigned = addStudentDTO.IsConfidentialityAgreementSigned, GraduationStatus = addStudentDTO.GraduationStatus, GraduationScore = addStudentDTO.GraduationScore, MentorGraduationThoughts = addStudentDTO.MentorGraduationThoughts, ProfessionId = addStudentDTO.ProfessionId, MentorId = addStudentDTO.MentorId } }; var result = await _userManager.CreateAsync(appUser, addStudentDTO.Password); if (!result.Succeeded) { throw new MilvaUserFriendlyException(string.Join("~", result.Errors.Select(m => m.Description))); } }
public void AddStudentToCourse(AddStudentDTO dto) { var course = (from c in _db.Courses join t in _db.CourseTemplate on c.TemplateID equals t.TemplateID where c.Semester == dto.Semester select c).SingleOrDefault(); if (course == null) { throw new KeyNotFoundException(); } var student = _db.Student.SingleOrDefault(x => x.SSN == dto.SSN); if (student == null) { throw new KeyNotFoundException(); } Entities.StudentInCourse toInsert = new Entities.StudentInCourse(); toInsert.CourseID = course.ID; toInsert.StudentID = student.SSN; _db.StudentInCourse.Add(toInsert); _db.SaveChanges(); }
public Student Createstd(AddStudentDTO stdDTO) { var STD = Mapper.Map <AddStudentDTO, Student>(stdDTO); repo.Add(STD); return(STD); }
private async void AddSTudent_Click(object sender, RoutedEventArgs e) { object @object; if (this.VM.IsItemSelected) { @object = new UpdateStudentDTO { Name = Name.Text, Surname = Surname.Text, StudentId = this.VM.StudentId, StudentNumber = this.VM.StudentNumber }; } else { @object = new AddStudentDTO { Name = Name.Text, Surname = Surname.Text, StudentNumber = Factory.RandomStudentNumberGenerator.Next(Config.StudentNumber_MinValue, Config.StudentNumber_MaxValue) }; } try { if (this.VM.IsItemSelected) { await AppData.Client.PutAsync(Config.REST_Endpoints_Students, @object); } else { await AppData.Client.Post(Config.REST_Endpoints_Students, @object); } await AppData.RefreshStudentsAsync(); this.VM.Students = AppData.Students; this.VM.Name = string.Empty; this.VM.Surname = string.Empty; this.VM.StudentNumber = 0; } catch (Exception ex) { string title = $"OH SNAP! Failed to add the following student : {((Student)@object).StudentNumber}"; string msg = $"Couldn't add student : {((Student)@object).ToString()}. \n Exception : {ex.Message}. \n Inner Exception : {ex.InnerException?.Message}."; var dialog = new MessageDialog(msg, title); await dialog.ShowAsync(); // Log error } }
public IHttpActionResult AddStudent(AddStudentDTO std) { if (ModelState.IsValid) { var CreatedStd = stdbl.Createstd(std); return(Ok(CreatedStd)); } else { return(BadRequest("Data is not valid")); } }
public IActionResult Post([FromBody] AddStudentDTO dto) { try { _studentManager.AddStudent(dto.Name, dto.GroupId); return(StatusCode(201)); } catch { return(StatusCode(500, new { message = "Unexpected Issue." })); } }
public async Task <IActionResult> Create(CreateStudentViewModel viewModel, int universityId) { var studentDto = new AddStudentDTO { FirstName = viewModel.FirstName, LastName = viewModel.LastName, AverageGrade = viewModel.AverageGrade, UniversityId = universityId }; var model = await _service.AddAsync(studentDto); if (model == null) { return(new JsonResult(new ReturnMessage { MessageContent = "Unexpected error when creating a student" }) { StatusCode = (int)HttpStatusCode.BadRequest }); } return(Ok(new StudentViewModel { StudentId = model.StudentId, FirstName = model.FirstName, LastName = model.LastName, AverageGrade = model.AverageGrade, UniversityId = model.UniversityId })); }
public async Task <ActionResult> AddStudentAsync(AddStudentDTO student) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var s = _mapper.Map <Student>(student); try { await this._service.AddAsync(s); await this._service.SaveAsync(); } catch (Exception ex) { return(Problem(detail: $"Failed to add student ({s.Name}). Error: {ex.Message}. InnerException: {ex.InnerException?.Message}")); } return(Ok()); }
public async Task <ActionResult> AddAsync(AddStudentDTO model) { try { if (!ModelState.IsValid) { ViewBag.Notification = new SuccessResult(false, "All fields are required!"); return(View(model)); } var result = await _studentsRepository.Add(model); if (result.Item1) { TempData["SuccessResultF"] = result.Item1; TempData["SuccessResultM"] = result.Item2; return(RedirectToAction("Index", "Students")); } else { ViewBag.Notification = new SuccessResult((bool)result.Item1, (string)result.Item2); model.Index = ""; var deps = _departamentsRepository.GetAll(); var statuses = _statusRepository.GetAll(); await Task.WhenAll(deps, statuses); ViewBag.DepartmentId = new SelectList(deps.Result.Item2, "Id", "DepartmentName"); ViewBag.StatusId = new SelectList(statuses.Result.Item2, "Id", "Name"); return(View(model)); } } catch (Exception ex) { throw ex; } }
public async Task <StudentDTO> AddAsync(AddStudentDTO addStudent) { try { var model = _db.Students.Add(new Student { UniversityId = addStudent.UniversityId, FirstName = addStudent.FirstName, LastName = addStudent.LastName, AverageGrade = addStudent.AverageGrade }).Entity; await _db.SaveChangesAsync(); return(new StudentDTO { StudentId = model.StudentId, FirstName = model.FirstName, LastName = model.LastName, AverageGrade = model.AverageGrade, UniversityId = model.UniversityId }); } catch { return(null); } }
public async Task <(bool, string)> Add(AddStudentDTO student) { using (var client = new HttpClient()) { DataMessage message = null; client.DefaultRequestHeaders.Clear(); client.BaseAddress = new Uri(WebAPIUrl); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(mediaType: "application/json")); var token = Session.GetString("Token"); if (string.IsNullOrEmpty(token)) { return(false, "You are not logged in!"); } client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(scheme: "Bearer", parameter: token); var responseMessage = await client.PostAsJsonAsync <AddStudentDTO>(requestUri : "/api/Students/AddStudent", student); var resultMessage = await responseMessage.Content.ReadAsStringAsync(); message = JsonConvert.DeserializeObject <DataMessage>(resultMessage); return(responseMessage.IsSuccessStatusCode, message.Message); } }
public async Task <IActionResult> AddStudent([FromBody] AddStudentDTO addStudent) { return(await _studentService.AddStudentAsync(addStudent).ConfigureAwait(false).GetObjectResponseAsync <AddStudentDTO>("Success").ConfigureAwait(false)); }