Esempio n. 1
0
        public async Task <ActionResult <StudentResponse> > Post(DTO.Student input, IFormFile file)
        {
            var studentExist = await _db.Students.Where(s => s.Username == input.Username).FirstOrDefaultAsync();

            if (studentExist != null)
            {
                return(Conflict(input));
            }

            var imageResponse = await _imageUploader.DataLoaderAsync(file, "userProfile");

            if (!imageResponse.IsSuccess)
            {
                return(BadRequest(imageResponse.Message));
            }

            var student = new Data.Student
            {
                Name            = input.Name,
                Bio             = input.Bio,
                ProfileImageUrl = imageResponse.Data,
                Username        = input.Username,
                EmailAddress    = input.EmailAddress,
            };

            _db.Students.Add(student);
            await _db.SaveChangesAsync();

            var result = student.MapStudentResponse();

            return(CreatedAtAction(nameof(GetStudent), new { username = result.Username }, result));
        }
        public static Student ToDto(this DbStudent that)
        {
            var result = new Student
            {
                Name = that.Name,
                MarkIds = that.Marks.Select(a => a.Id).ToList(),
                StudentId = that.Id,
                Surname = that.Surname,
                SubjectIds = that.Subjects.Select(a => a.Id).ToList(),
                AverageMarks = new Dictionary<string, double>()
            };


            foreach (var subject in that.Subjects)
            {
                var marks = that.Marks.Where(a => a.SubjectId == subject.Id).ToArray();
                var averageMark = (double) marks.Sum(a => a.Value)/marks.Length;

                result.AverageMarks[subject.Id.ToString()] = averageMark;
            }

            result.ComonAverageMark = (double) that.Marks.Sum(a => a.Value)/that.Marks.Count();

            return result;
        }