Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
using SingerAPI_6.Entities;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MySqlX.XDevAPI;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using System.Linq;
using Microsoft.AspNetCore.Authorization;
using Org.BouncyCastle.Tls;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
using System.ComponentModel.DataAnnotations;

namespace SingerAPI_6.Controllers
{
    [Authorize]
    [ApiController]
    [Route("api/[controller]")]
    
    public class UserController : ControllerBase
    {
        private readonly DBContext DBCon;

        public UserController(DBContext DBConte)
        {
            DBCon = DBConte;
        }

        // We perform here a GET Method for All record
        [HttpGet("GetUsers")]
        public async Task<ActionResult<List<User>>> GetUser()
        {
            var list = await DBCon.department.Select(
                s => new User()
                {
                    id = s.id,
                    FirstName = s.FirstName,
                    LastName = s.LastName,
                    Gender = s.Gender,
                    DeptName = s.DeptName
                }
            ).ToListAsync();

            if (list.Count < 0)
            {
                return NotFound();
            }
            else
            {
                return list;
            }
        }

        [HttpGet("GetUserbyid")]
       
        public async Task<ActionResult<User>> GetUserbyid(int id , Stu stu)
        {
            
                User user = await DBCon.department.Select(
                    s => new User
                    {
                        id = s.id,
                        FirstName = s.FirstName,
                        LastName = s.LastName,
                        Gender = s.Gender,
                        DeptName = s.DeptName,

                    })
                    .FirstAsync(s => s.id == id);
           
                if (user == null)
                {
                    return NotFound();
                }

                else if(stu == Stu.First)
                {
                    return Ok(user);
                }
                else if(stu == Stu.All)
                {
                  var list = await DBCon.department.Select(
                    s => new User()
                    {
                       id = s.id,
                       FirstName = s.FirstName,
                       LastName = s.LastName,
                       Gender = s.Gender,
                       DeptName = s.DeptName
                    }
                    ).ToListAsync();

                    if (list.Count < 0)
                    {
                       return NotFound();
                    }
                    else
                    {
                       return Ok(list);
                    }  
                }
                else if(stu == Stu.Last)
                {
                  User users = await DBCon.department.Select(
                 s => new User
                 {
                     id = s.id,
                     FirstName = s.FirstName,
                     LastName = s.LastName,
                     Gender = s.Gender,
                     DeptName = s.DeptName,

                 })
                 .LastAsync(s =>s.id == id);

                 if(users == null)
                 {
                    return NotFound();
                 }
                 else
                 {
                    return users;
                 }
                }
                else
                {
                    return NotFound();
                }
        }

       

        [HttpPost("insertData")]
        public async Task<HttpStatusCode> AddData([FromForm]User User)
        {
            var entity = new User()

            {
                id = User.id,
                FirstName = User.FirstName,
                LastName = User.LastName,
                Gender = User.Gender,
                DeptName = User.DeptName,
               FileUpload = User.FileUpload
               
              
            };

            var saveFilePath = Path.Combine("c:\\savefilepath\\", User.FileUpload.FileName);
            using (var stream = new FileStream(saveFilePath, FileMode.Create))
            {
                User.FileUpload.CopyToAsync(stream);
            }
            
            DBCon.department.Add(entity);
            await DBCon.SaveChangesAsync();

            return HttpStatusCode.Created;
            
        }





        [HttpPut("UpdateUser")]
        public async Task<HttpStatusCode> UpdateUser(User User)
        {
            var entity = await DBCon.department
                .FirstOrDefaultAsync(s => s.id == User.id);

            entity.id = User.id;
            entity.FirstName = User.FirstName;
            entity.LastName = User.LastName;
            entity.Gender = User.Gender;
            entity.DeptName = User.DeptName;
            await DBCon.SaveChangesAsync();
            return HttpStatusCode.OK;
                 
        }

        [HttpPut("UpdateUserDept")]
        public async Task<HttpStatusCode> UpdateUserDept(User User)
        {
            var entity = await DBCon.department.FirstOrDefaultAsync(s => s.id == User.id);

            entity.DeptName = User.DeptName;
            await DBCon.SaveChangesAsync();
            return HttpStatusCode.OK;
        }
       

        [HttpDelete("deletebyId")]
        public async Task<HttpStatusCode> DeleteUser(int id)
        {
            var entity = new User()
            {
                id = id
            };
            DBCon.department.Attach(entity);
            DBCon.department.Remove(entity);
            await DBCon.SaveChangesAsync();
            return HttpStatusCode.OK;
        }
    }


}


What I have tried:

using Microsoft.AspNetCore.Mvc;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace SingerAPI_6.Entities
{
    public class User
    {
        public int id { get; set; }
        public string? FirstName { get; set; }

        public string? LastName { get; set; }
        public string? Gender { get; set; }
        public string? DeptName { get; set; }

        [NotMapped]
        public IFormFile  FileUpload { get; set; }   
    }
}
Posted
Comments
Richard MacCutchan 28-Nov-22 6:23am    
What is the error and where does it occur? Just dumping your code with no explanation is not the best way to get help.
Dave Kreskowiak 28-Nov-22 14:43pm    
You have to show the exact exception message that the code threw AND THE INNER EXCEPTION MESSAGE.

Without both of those messages, you're not going to get an answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900